push view controller after dismissing presented vi

2019-04-16 18:40发布

问题:

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:

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];
    }];

}


回答2:

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];
}];

}