dismiss modalviewcontroller from another modalviewcontroller

蹲街弑〆低调 提交于 2019-11-27 23:21:11

Its like this.

A presents B. Here, A is parent of B (Here, A.modalViewController will be B and B.parentViewController will be A)

And B presents C. Here, B is parent of C (Here, B.modalViewController will be C and C.parentViewController will be B)

According to apple guidelines, its responsibility of parent controller to dismiss its child controller.

So if you want to dismiss controller C, you call dismissModalViewController at C.parentViewController. As C's parent is B, thus B is dismissing its modal (child) controller that it presented.

But you want to even dismiss B. Its responsibility of B's parent to dismiss B. So you need to say [B.parentViewController dismissModalViewControllerAnimated: YES];

Thus, you need to get B from C as C.parentViewController (Don't forget to typecast here). Then you say that [B.parentViewController dismissModalViewControllerAnimated: YES];

Try [self.parentViewController dismissModalViewControllerAnimated:YES];

This will dismiss both modal view controllers

The dismissModalViewControllerAnimated: method is part of the UIViewController class, not the of UIView. So you need to do

[self.parentViewController dismissModalViewControllerAnimated:YES];

instead of calling it on self.view.superview.

To clarify even further, what you will probably need is something like this:

[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

i just found it's answer, may be it will help somebody we need just one line of code

[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];

Sadly it the view always flick to the first modal in the stack then perform the animation.

I fixed it with a custom animation:

        let transition: CATransition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionReveal
        transition.subtype = kCATransitionFromBottom
        self.view.window!.layer.add(transition, forKey: nil)
        self.presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!