Dismiss or remove previous modally presented view controller as soon as the next one appear modally

So, let's assume that you have a storyboard similar to:

enter image description here

What should happens is:

  • Presenting the the second ViewController (from the first ViewController).
  • Presenting the the third ViewController (from the second ViewController).
  • dismissing to the first ViewController (from the third ViewController).

In the third ViewController button's action:

@IBAction func tapped(_ sender: Any) {
    presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}

As you can see, by accessing the presentingViewController of the current ViewController, you can dismiss the previous hierarchy of the view controllers:

The view controller that presented this view controller.

By implementing presentingViewController?.presentingViewController? that means that: the presented of the presented current ViewController :)

It might seem a little bit confusing, but it is pretty simple.

So the output should be like (I added background colors to the viewControllers -as vc1: orange, vc2: black and vc3: light orange- to make it appears clearly):

enter image description here

EDIT:

If you are asking to remove the ViewController(s) in the middle (which in this example the second ViewController), dismiss(animated:completion:) does this automatically:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

Referring to what are you asking:

I think even if I want to dismiss current view, the previous one will still remain appear when current one dismiss.

I think that appears clearly on the UI (and I find it ok), but as mentioned in the dismiss documentation discussion, both the third and the second will be removed from the stack. That's the right way.


What you want is an "unwind segue":

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW8

https://developer.apple.com/library/archive/technotes/tn2298/_index.html

It allows you to dismiss multiple view controllers at the same time, without having to know how many there are in the stack.

In VC1 you would implement an IBAction called (for instance) unwindToRoot. Then in the storyboard for VC3, you wire up your Done button to the Exit object and choose the unwindToRoot action.

When that button is pressed, the system will dismiss all the view controllers it needs to bring you back to VC1.

This is better than calling presentingViewController?.presentingViewController?.dismiss(), because VC3 doesn't need to know anything about the view controller hierarchy underneath it.