2

I have this navigation stack

RootVC ---> VC1 --> (presenting)-> ModalVC

and I have VC2 (not in navigation stack).

When presenting ModalVC, I want to click on button in my ModalVC to dismiss ModalVC, then push VC2 into the navigation stack after VC1 at one click. It should look like this:

RootVC ---> VC1 ---> VC2

I tried a lot of methods to make it, but pushing event fire only, when I return to my RootVC.

I tried to make it with delegates:

In ModalVC on click:

[self dismissViewControllerAnimated:YES completion:^{
   if ([self.delegate respondsToSelector:@selector(dismissAndPush:)]) {
       [self.delegate performSelector:@selector(dismissAndPush:) withObject:VC2];
   }
}];

In VC1:

- (void)dismissAndPush:(UIViewController *)vc {
    [self.navigationController pushViewController:vc animated:NO];

}

Please help to understand this behavior. Where is my mistake?

1

2 Answers 2

1

From Apple Documentation:

The presenting view controller is responsible for dismissing the view controller it presented.

So, VC1 should be dismissing the ModalVC, try to do this

ModalVC on click:

   if ([self.delegate respondsToSelector:@selector(dismissAndPush:)]) {
       [self.delegate performSelector:@selector(dismissAndPush:) withObject:VC2];
   }

In VC1:

- (void)dismissAndPush:(UIViewController *)vc {

[self dismissViewControllerAnimated:YES completion:^{
    [self.navigationController pushViewController:vc animated:NO];
}];

}
1
  • Check if parameter vc is instantiated on dismissAndPush:
    – Leonardo
    Feb 12, 2015 at 11:42
1

Error has been in other. If Im right understand: some animations before dismissing presented view controller are blocking animations in navigation stack. I solved this problem with 2 ways:

1) deleteting or set right animations before dismissing

2) use setViewControllers in navigation controller (I select it)

- (void)dismissAndPush:(UIViewController *)vc {
    [self dismissViewControllerAnimated:NO completion:^{
        NSMutableArray *mutableControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        NSArray *controllers = [mutableControllers arrayByAddingObject:vc];
        [self.navigationController setViewControllers:controllers animated:NO];
    }];

}
1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.