Navigation Controller
Drag View and change it to navigation view controller.
i ) create two more viewcontroller.h,viewcontroller.m an viewcontroller.xib 's and map each other correctly then Insert headings for Views, Labels and buttons.
ii) There is no need to change Main.m, AppDelgate.h.
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(testingAppLevelNotificaions) name:@"test" object:nil];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
[[NSNotificationCenter defaultCenter]addObserver:self.viewController selector:@selector(test2) name:@"test" object:nil];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController{
}
-(IBAction)goToSecond;
-(void)test2;
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
-(IBAction)goToSecond{
SecondViewController *second = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:second animated:YES];
}
-(void)test2{
NSLog(@"test2 method called");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"Root";
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
-(IBAction)goToThird;
@end
#import "ViewController.h"
@implementation ViewController
-(IBAction)goToSecond{
SecondViewController *second = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:second animated:YES];
}
-(void)test2{
NSLog(@"test2 method called");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"Root";
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
-(IBAction)goToThird;
@end
SecondViewController.m
#import "SecondViewController.h"
#import "ThirdViewController.h"
@implementation SecondViewController
-(IBAction)goToThird{
ThirdViewController *third = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
[self.navigationController pushViewController:third animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = @"Second";
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(test3) name:@"test" object:nil];
}
-(void)test3{
NSLog(@"test3 called");
}
@end
ThirdViewController.h
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController{
IBOutlet UILabel *lbl;
}
-(IBAction)postNotification;
@end
#import "SecondViewController.h"
#import "ThirdViewController.h"
@implementation SecondViewController
-(IBAction)goToThird{
ThirdViewController *third = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
[self.navigationController pushViewController:third animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = @"Second";
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(test3) name:@"test" object:nil];
}
-(void)test3{
NSLog(@"test3 called");
}
@end
ThirdViewController.h
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController{
IBOutlet UILabel *lbl;
}
-(IBAction)postNotification;
@end
ThirdViewController.m
#import "ThirdViewController.h"
@implementation ThirdViewController
-(IBAction)postNotification{
lbl.text=@"Third view controller";
[[NSNotificationCenter defaultCenter]postNotificationName:@"test" object:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
@end
tags: iphone sample codes, iphone sample projects, sample apps for iphone with source code, sample programs in iphone,

Posted in:
0 comments:
Post a Comment