16

I am trying to push three view controllers onto the navigation controller.

  [self.navigationController pushViewController:one animated:YES];
  [self.navigationController pushViewController:two animated:YES];
  [self.navigationController pushViewController:three animated:YES];

The desired behavior is that view three will show, and when the back button is pressed it will go to view two and then to view one...

What actually happens is that view one is visible and pressing back goes to view two and then back again it goes to view one. Which is to say that view one is shown instead of view three.

Very strangely, looking at the viewController array of the navigationController after the calls above show the right entries, and looking at the visibleViewController property shows that it has view three in it... even though view one is visible.

If I navigate to a sub view from the visible view one (that shows in the place of view three) and press back from that sub view... it goes to view three.

It looks like it is showing view one, but knows it is on view three...

I am completely confused... any ideas?

Jim

2 Answers 2

35

For the first two pushes, don't pass the animated flag in as YES, set it to NO:

[self.navigationController pushViewController:one animated: NO]; 
[self.navigationController pushViewController:two animated: NO];
[self.navigationController pushViewController:three animated: YES];

This will give you the effect you want. Otherwise, you're confusing the animation system, as it tries to animate three views into the same space.

Sign up to request clarification or add additional context in comments.

5 Comments

Outstanding. Thanks Ben! jim
Great answer, if it was my question I would accept this one. Fixed it for me with XCode 5 and iOS 7.
Thanks for that, too bad OP never accepted it because it's the correct answer. Wonder if the mods can do that?
Hi Ben If I have to pass data along push how I will pass it from appdelegate
this will first show screen 2 instantaneously and then transition to screen 3 smoothly. But you usually want to have a smooth transition from the current view to screen 3 whithout showing anything else. To do that, you have to use the setViewControllers method, as explained by @KimAMartinsen
16

The problem with the current most upvoted answer is that one and two will be visible in a split second before the third becomes visible. Not a huge problem, but it'll not make a good impression on the user. The solution you're looking for:

NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
[controllers addObject:one];
[controllers addObject:two];
[controllers addObject:three];
[self.navigationController setViewControllers:controllers animated:YES];

This will animate in three without one or two becoming visible in the process.

2 Comments

This answer provides the desired effect, as the whole purpose of setting the animated flag to "no" is that I don't want to see the first and second view controllers.
THIS IS ACTUALLY THE CORRECT ANSWER

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.