CodeGuy
CodeGuy

Reputation: 28907

UINavigationController - basics

I'm trying to use a UINavigationController but I'm uncertain how. Up till now (for about a year), I've been using presentModalViewController and dismissModalViewController to present/dismiss view controllers.

So, this is what I did. My main view controller (the first one that shows on launch) is called MainViewController, and it extends UIViewController.

So I made this launch function in my app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MainViewController *controller = [[MainViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];  
    [self.window addSubview:navigationController.view];  
    [self.window makeKeyAndVisible];

    return YES;
}

And in my MainViewController's viewDidLoad method:

- (void)viewDidLoad {

        [super viewDidLoad];

        self.title = @"Title";
        self.navigationController.navigationBar.tintColor = [Constants barColor];

            ....more code...
    }

But, in my MainViewController, I'd like to present another view controller called SecondViewController, which needs a UINavigationBar with a back arrow button. So do I make SecondViewController extend UIViewController and do the same thing by setting the title and backButton in the viewDidLoad method? And how do I present it? What should I do to accomplish this?

Upvotes: 0

Views: 654

Answers (3)

niceamitsingh
niceamitsingh

Reputation: 41

UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease. Please visit "How to add UINavigationController Programmatically"

Upvotes: 1

Alex Coplan
Alex Coplan

Reputation: 13361

You'll need to set a root view controller up, it's easiest starting from the apple template.

Here's where the magic happens:

UIViewController *controller = [[UIViewController alloc] initWithNibName:@"MyNib" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

The nav controller does all the work for you (back buttons, titles, animations) - it keeps track!


My workflow is this:

Setup MutableArray in the viewDidLoad, add controllers to it, e.g:

NSMutableArray *array = [[NSMutableArray alloc] init];
MyCustomViewController *customView = [[MyCustomViewController alloc] initWithNibName:@"nib" bundle:@"nil"];
customView.title = @"Second Level";
[array addObject:customView];
self.controllers = array;

Then in your delegate:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    UIViewController *childControllerToBe = [controllers objectAtIndex:row];
    [self.navigationController pushViewController:childControllerToBe animated:YES];
}

This, along with a lot more can be learnt by reading a decent beginner book such as Beginning iPhone Development

Also, apple docs are always good :)

Upvotes: 1

Sum
Sum

Reputation: 4399

You should Push it onto the navigation stack.

This Lecture by Stanford's iPhone Course will teach you a lot about Navigation Bars. (It's a quick read)

Basically at the heart of it you need this code:

[self.navigationController pushViewController:SecondView];

You can use PopViewController to go back programmatically, but the Back Button is automatically created.

Here's some source code from the Lecture. It covers exactly what you are having issues with.

Upvotes: 0

Related Questions