0
votes

I'm trying to push a certain numbers of controllers at the same time using the prepareForSegue method, but getting this error:

nested push animation can result in corrupted navigation bar and Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

Here the code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
    if ([[segue identifier] isEqualToString:@"rawSegue"]){
        MyController * aController = [segue destinationViewController];
        [aController setText:theText];
        [aController setType:Type];
        [aController setCountry:country];
    }
}

The prepareForSegue method is called for each object contained in an NSMutableArray. How can I call multiple view controllers without getting this error? The Segue is wired in the storyboard with the correct identifier.

2
Can you paste the exact code that you are using to push view controllerBurhan Ahmad
The question is not clear, you want to push two controllers at the same time using segue ? Display multiple ViewControllers at the same time or open controllers queued ?Atika
The warning says all about it. If you push multiple view controllers before ending one transition may cause currupted navigation bar. That is if you press the back button, it may pop top view controller and you may not be able to pop other view controllers OR you can pop one view controller and there may be back button which jhave no effect etc.Johnykutty
So basically the answer is that I cannot push multiple view controllers with the same prepareForSegue , is that correct?yorh

2 Answers

0
votes

If you want to push multiple UIViewControllers into navigation Controller, you should add all without animation except last. e.g.

/**
 A method to push multiple UIViewControllers into UINavigationController. 
 You can push multiple controller, but the trick is you push them without animation, 
 only last is pushed with animation.
 @param arrayControllers An array of UIViewController to be pushed at once in Navigation Controller
 */
-(void)pushMultipleViewControllers:(NSMutableArray<UIViewController *> *)arrayControllers
{
    int index = 0;
    for (UIViewController *controller in arrayControllers) {
        BOOL isAnimated = NO;
        if(index==arrayControllers.count-1){
            //Only last view controller should be inserted with animation
            isAnimated=YES;
        }
        [self.navigationController pushViewController:controller animated:isAnimated];
        index++;
    }
}
0
votes

There is a more concise way to do the above answer:

- (void)pushMultipleViewControllers:(NSMutableArray<UIViewController *> *)arrayControllers {

    NSMutableArray *navigationControllerStack = self.navigationController.viewControllers.mutableCopy;

    [navigationControllerStack addObjectsFromArray:arrayControllers];

    [self.navigationController setViewControllers:navigationControllerStack animated:YES];

}

Note, this still doesn't address how to do it with a segue, but neither does the answer above.