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 view开发者_StackOverflow中文版Controller2 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?
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.
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.
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.
精彩评论