push view controller after dismissing presented view controller

青春壹個敷衍的年華 提交于 2019-12-22 01:05:46

问题


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

}


来源:https://stackoverflow.com/questions/28451295/push-view-controller-after-dismissing-presented-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!