Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump to next controller with UINavigationController on iPhone

The program has a nav bar and normally when clicking a button in viewController1 it goes to viewController2. when clicking a button in viewController2 it goes to viewController3. and the user can navigate back from viewController3 to viewController2 then to viewController1 using the back button in the navigation bar.

I want to make a button that programatically takes the user directly to viewController3 from viewController1. then the user can navigate back from viewController3 to viewController2 to viewController1.

Is there a way to push two views into the navigation controller? or is another way to achieve the desired behavior? how should i design this?

like image 343
Yazzmi Avatar asked Aug 24 '10 02:08

Yazzmi


2 Answers

Sorry for misreading your question. Because you want to push 2 view controller and then go back 1 by 1. I think the solution now is simple.

You only need to push view controller 2 times, 1 without animation and 1 with animation like this:

[viewController1.navigationController pushViewController:viewController2 animated:NO];
[viewController2.navigationController pushViewController:viewController3 animated:YES];

So, for the user, it happens like you only push 1 but in behind the scene, you actually push 2 view controllers. Then when you want to come back, just need to pop 1 by 1.

like image 188
vodkhang Avatar answered Nov 10 '22 23:11

vodkhang


You can directly set the navigation stack on a UINavigationController using . setViewControllers:animated:.

For example, assuming this code is somewhere in viewController1 like the handler for a button press in it's view:

NSMutableArray* viewControllers = [self.navigationController.viewControllers mutableCopy];
UIViewController* controller = [[MyViewController2 alloc] init];
[viewControllers addObject:controller];
[controller release];
controller = [[MyViewController3 alloc] init];
[viewControllers addObject:controller];
[controller release];
[self.navigationController setViewControllers:viewControllers animated:YES];

The creation business at the top of that with the mutableCopy call is so that you're preserving whatever is already on the navigation stack.

like image 45
imaginaryboy Avatar answered Nov 10 '22 21:11

imaginaryboy