iOS8 issue when trying to push multiple UIViewCont

2019-06-07 10:59发布

问题:

I have 6 UIViewControllers connected with push segues with identifiers and a functionality in my app to 'jump' to the desired UIViewController using this method for stacking ViewControllers:

- (void) JumpTo6 {
UINavigationController *nav = self.navigationController;
UIViewController *a =[self.storyboard instantiateViewControllerWithIdentifier:@"2"];
[nav pushViewController:a animated:NO];
UIViewController *b =[self.storyboard instantiateViewControllerWithIdentifier:@"3"];
[nav pushViewController:b animated:NO];
UIViewController *c =[self.storyboard instantiateViewControllerWithIdentifier:@"4"];
[nav pushViewController:c animated:NO];
UIViewController *d =[self.storyboard instantiateViewControllerWithIdentifier:@"5"];
[nav pushViewController:d animated:NO];
UIViewController *e =[self.storyboard instantiateViewControllerWithIdentifier:@"6"];
[nav pushViewController:e animated:YES];

When using iOS7 everything worked fine. I would fire this method, and let's say I was on UIViewController one, the system would stack every UIViewController up to UI VC 6 and UIViewController six would get presented with the animation.

But on iOS8 strange behaviour appears. The system shows me the UIViewcontroller 5 for a brief period of time, and then goes to UIViewcontroller 6. This is something I don't want.

To sum things up:

iOS 7: 1 -----> 6 - desirable

iOS 8: 1 -----> 5 (for a brief period of time) ----> 6 - undesirable

My question is how to achieve desirable effect using iOS 8. Ty!

回答1:

Use the method:

- (void)setViewControllers:(NSArray *)viewControllers
              animated:(BOOL)animated

to set all the controllers at a time showing only the last.

In your case:

- (void) JumpTo6 {
  UINavigationController *nav = self.navigationController;
  UIViewController *a = [self.storyboard instantiateViewControllerWithIdentifier:@"2"];
  UIViewController *b = [self.storyboard instantiateViewControllerWithIdentifier:@"3"];
  UIViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:@"4"];
  UIViewController *d = [self.storyboard instantiateViewControllerWithIdentifier:@"5"];
  UIViewController *e = [self.storyboard instantiateViewControllerWithIdentifier:@"6"];
  NSArray *viewControllers = nav.viewControllers;
  NSArray *newViewControllers = [NSArray arrayWithObjects:a, b, c, d, e, nil];
  [nav setViewControllers:[viewControllers arrayByAddingObjectsFromArray:newViewControllers] animated:YES];
}