views:

48

answers:

3

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?

A: 

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.

vodkhang
where would i put popToViewController? in viewcontroller1 or viewcontroller2
Yazzmi
in viewcontroller 3, when you handle the action of the button.
vodkhang
you said "Pops view controllers until the specified view controller is at the top of the navigation stack." where do you specify the view controller?
Yazzmi
Look at the method signature again. popToViewController:animated:. You have to pass viewController1 into the argument
vodkhang
He's trying to PUSH multiple controllers, not pop multiple controllers.
imaginaryboy
ops, blame me. I misread it. Will have to edit my one
vodkhang
A: 

It is possible to push 2 view controllers at once but once you push the first one the navigationController property on the current ViewController will become nil as it is no longer on top of the stack so you will have to make a reference to it. I haven't ever tried to push 2 at a time with animations, I'm not sure how that would look.

skorulis
actually I don't really need to show viewController2 just want to make sure I can come back to viewController2. 3->2->1
Yazzmi
You have to push it onto the navigation controllers stack at some point.
skorulis
If you push two controllers, the first one pushed absolutely will NOT become ni because the navigation controllers still has a reference to it in the stack of pushed controllers.
imaginaryboy
A: 

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.

imaginaryboy
do you need to release viewControllers? if so where? thx
Yazzmi
Where you release them likely depends on where you retained them or allocated them. If you don't need a separate reference to them, then you can release them as soon as you've put them into the array as I did in the example code above.
imaginaryboy