Joseph
Joseph

Reputation: 207

Adding a UINavigationController to a view-based application

So, I started developing an app in an entirely view-based way in Xcode 3.2.5 and then upgraded to Xcode 4.4. I have one NIB file (well two, MainWindow.xib and my default view controller's .xib). I have a bunch of view controllers that up til now I've been presenting via presentModalViewController.

The problem is that now I want one of those views to be navigation based, which is to say, when the user enters it, they get the top navigation bar, and everything they get to from that point forward is done through a navigation controller. And when they're done and have backed out of it completely, they get back to the regular non-navigation-controller-using views.

It seems like this is a common question that no one has fully described the answer to. Either that or responses which seem helpful like How to add a navigation controller to a view-based application? are way too vague for me. I'm basically looking for a step-by-step explanation of how you add a UINavigationController to your project for presenting only a few of the views in it.

Upvotes: 2

Views: 773

Answers (1)

Bill Burgess
Bill Burgess

Reputation: 14154

If you already have your view working as you intended it, adding a nav controller to modal view is pretty simple.

NewViewController *newView = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
UINavigationController *navView = [[UINavigationController alloc] initWithrootViewController:newView];
[self presentModalViewController:navView animated:YES];

Your modal view will inherit the navigation bar and all the attributes for presenting more views inside that modal. When you are done with the modal, just dismiss it.

To load more views on top of the nav controller is pretty easy.

AnotherViewController *anotherView = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
[self.navigationController pushViewController:anotherView animated:YES];

To manually pop the view controller from the stack:

// note, you won't need to call this for the auto created back button, that is handled for you
// this would only be if you wanted manual control over going back outside the back button
[self.navigationController popViewControllerAnimated:YES];

Once you are all done with the modal view, you can call this from anywhere to drop it out of sight, returning you to your original view. Handy for multiple detail screens, signup process, etc.

[self.navigationController dismissModalViewControllerAnimated:YES];

Upvotes: 1

Related Questions