Dabrut
Dabrut

Reputation: 862

iOS - How can I initialize a UINavigationController to display a pushed ViewController?

I'm wondering how can I initialize a UINavigationController in order to display the third view controller in the stack ? Kinda like the Mail app. Even if you kill the app, when you launch it, you see the view controller containing all your mails and there the "back" button allowing you to the list of your mailboxes.

Thanks for your answers.

Upvotes: 3

Views: 7405

Answers (4)

MattyG
MattyG

Reputation: 8497

You would still init the navigation controller like normal, but then push your second and third view controllers onto it before it displays. So if you're doing this in your app delegate's application: didFinishLaunchingWithOptions:

UIViewController *firstController = [[[FirstViewCon alloc] initWithNibName:@"MyFirstViewCon" bundle:nil] autorelease];
UIViewController *secondController = [[[SecondViewCon alloc] initWithNibName:@"SecondViewCon" bundle:nil] autorelease];
UIViewController *thirdController = [[[ThirdViewCon alloc] initWithNibName:@"ThirdViewCon" bundle:nil] autorelease];

UINavigationController *theNavCon = [[[UINavigationController alloc] initWithRootViewController:firstController] autorelease];

[theNavCon pushViewController:secondController animated:NO];
[theNavCon pushViewController:thirdController animated:NO];

self.window.rootViewController = theNavCon;

Upvotes: 0

sch
sch

Reputation: 27506

You have to store in UserDefaults the state of your app when it exists. You can do that using the app delegate method - (void)applicationWillTerminate:(UIApplication *)application.

Then when the app is launched again, you retrieve that information from UserDefaults, and initialize the view controllers you want to have in the view controllers hierarchy. Then you add them to the UINavigationController using the method: setViewControllers:animated:.

Upvotes: 2

Kirby Todd
Kirby Todd

Reputation: 11556

Use popToViewController.

navigationController.viewControllers = [NSArray arrayWithObjects:firstVC, secondVC, thirdVC, nil];
[navigationController popToViewController:thirdVC animated:NO];

NOTE: make sure animated is set to NO.

Upvotes: 1

Mutix
Mutix

Reputation: 4196

You may want to look into the UINavigationController's setViewControllers:animated: method :

Parameters

viewControllers

The view controllers to place in the stack. The front-to-back order of the controllers in this array represents the new bottom-to-top order of the controllers in the navigation stack. Thus, the last item added to the array becomes the top item of the navigation stack.

animated

If YES, animate the pushing or popping of the top view controller. If NO, replace the view controllers without any animations.

Discussion

You can use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In addition, this method lets you update the set of controllers without animating the changes, which might be appropriate at launch time when you want to return the navigation controller to a previous state.

If animations are enabled, this method decides which type of transition to perform based on whether the last item in the items array is already in the navigation stack. If the view controller is currently in the stack, but is not the topmost item, this method uses a pop transition; if it is the topmost item, no transition is performed. If the view controller is not on the stack, this method uses a push transition. Only one transition is performed, but when that transition finishes, the entire contents of the stack are replaced with the new view controllers. For example, if controllers A, B, and C are on the stack and you set controllers D, A, and B, this method uses a pop transition and the resulting stack contains the controllers D, A, and B.

Availability

Available in iOS 3.0 and later.

UINavigationController Class Reference

Upvotes: 6

Related Questions