简体   繁体   中英

Problem pushing multiple view controllers onto navigation controller stack

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM