Sunday, June 20, 2010

World's Simplest Example: UINavigationController

This is a Simplest Example of how to use a UINavigationController (based on my question here). The idea is that the example is 100% self contained. Warning: if you actually code this way (big monolithic classes) your code will suck. In this example, the UIViewControllers should actually be subclasses that contain the behavior and data they need to do their work. But that's another problem.

 - (void)viewDidLoad {
[super viewDidLoad];

UIViewController *one = [[[UIViewController alloc] init] autorelease];

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

= [[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];

= [[[UIViewController alloc] init] autorelease];

[one.view setBackgroundColor:[UIColor blueColor]];
[one setTitle:@"Two"];

// subsequent views get pushed, pulled, prodded, etc.
[navController pushViewController:one animated:YES];

No comments: