Thursday, March 1, 2012

UINavigationController - A Simple Example

Recently I decided to use the UINavigationController class so I could push and pop sub views for easy navigation. As usual, all I wanted was a simple clean example to help me get started and of course all the examples I found were either so simple they were useless or they were trying to do much more than the simple example I wanted, as always, here is what I came up with (with base code being copied from some simple example).

This example shows how to navigate between 3 sub views. It includes forward and backward buttons where appropriate.

First the .h file ...




#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
UINavigationController * navController;
UIBarButtonItem *nextButtonOne;
UIBarButtonItem *nextButtonTwo;
UIBarButtonItem *prevButtonTwo;
UIBarButtonItem *prevButtonThree;
UIViewController *one;
UIViewController *two;
UIViewController *three;
int currentView;
}

@property (nonatomic, retain)UINavigationController * navController;
@property (nonatomic, retain)UIBarButtonItem *nextButtonOne;
@property (nonatomic, retain)UIBarButtonItem *nextButtonTwo;
@property (nonatomic, retain)UIBarButtonItem *prevButtonTwo;
@property (nonatomic, retain)UIBarButtonItem *prevButtonThree;
@property (nonatomic, retain)UIViewController *one;
@property (nonatomic, retain)UIViewController *two;
@property (nonatomic, retain)UIViewController *three;
@property (atomic, assign)int currentView;

- (IBAction)handleNextButton:(id)sender;
- (IBAction)handlePrevButton:(id)sender;

@end




And next, the .m file ...





#import "ViewController.h"

@implementation ViewController

@synthesize navController, nextButtonOne, nextButtonTwo, currentView;
@synthesize prevButtonTwo, prevButtonThree, one, two, three;

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (IBAction)handleNextButton:(id)sender {
if (currentView == 0){
[navController pushViewController:two animated:YES];
}else{
[navController pushViewController:three animated:YES];
}
currentView++;
}

- (IBAction)handlePrevButton:(id)sender {
[navController popViewControllerAnimated:YES];
currentView--;
}

- (void)viewDidLoad {
[super viewDidLoad];

one = [[UIViewController alloc] init];

[one.view setBackgroundColor:[UIColor yellowColor]];
[one setTitle:@"One"];

nextButtonOne = [[UIBarButtonItem alloc]
initWithTitle:@"Two"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handleNextButton:)];

[[one navigationItem] setRightBarButtonItem:nextButtonOne];


navController = [[UINavigationController alloc] initWithRootViewController:one];
// here 's the key to the whole thing: we're adding the navController's view to the
// self.view, NOT the one.view! So one would be the home page of the app (or something)
[self.view addSubview:navController.view];

two = [[UIViewController alloc] init];
[two.view setBackgroundColor:[UIColor blueColor]];
[two setTitle:@"Two"];

nextButtonTwo = [[UIBarButtonItem alloc]
initWithTitle:@"3"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handleNextButton:)];

prevButtonTwo = [[UIBarButtonItem alloc]
initWithTitle:@"1"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handlePrevButton:)];

[[two navigationItem] setLeftBarButtonItem:prevButtonTwo];
[[two navigationItem] setRightBarButtonItem:nextButtonTwo];

[navController pushViewController:two animated:YES];

three = [[UIViewController alloc] init];
[three.view setBackgroundColor:[UIColor redColor]];
[three setTitle:@"Three"];

prevButtonThree = [[UIBarButtonItem alloc]
initWithTitle:@"2"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handlePrevButton:)];

[[three navigationItem] setLeftBarButtonItem:prevButtonThree];
[navController pushViewController:three animated:YES];

currentView = 2;
}


- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}

@end




Its that simple :-)