Single Function to Dismiss All Open View Controllers

single function to dismiss all open view controllers

You can call :

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

Should dismiss all view controllers above the root view controller.

iPhone - dismiss multiple ViewControllers

I found the solution.

Of course you can find the solution in the most obvious place so reading from the UIViewController reference for the dismissModalViewControllerAnimated method ...

If you present several modal view
controllers in succession, and thus
build a stack of modal 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.

so it's enough to call the dismissModalViewControllerAnimated on the target View.
I used the following code:

[[[[[self parentViewController] parentViewController] parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];

to go back to my home.

Dismiss all modals in iOS with Swift 4

The way you are dismissing a ViewController is not the correct way. The presenting view controller is responsible for dismissing the view controller. Ideally you have to implement a protocol in your presenting ViewController and , dismiss your modal from your 'presenting' ViewController not 'presented' ViewController.

The reason why your way still works is, when a ViewController calls self.dimiss if there's nothing to dismiss UIKit will delegate it back to its parent. If you implement this correct way, once you dismiss , your presenting viewcontroller will dismiss , hence all the presented viewcontrollers will be dismissed instead of the last one.

From Apple Docs:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

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.

If you want to retain a reference to the view controller's presented view controller, get the value in the presentedViewController property before calling this method.

The completion handler is called after the viewDidDisappear(_:) method is called on the presented view controller.

Dismiss all view controllers below current view controller swift

From VC2, use setViewControllers method to push VC3 and to remove the rest,

self.navigationController?.setViewControllers([VC3], animated: true)

Change root view controller and dismiss all view controllers

You do it, in essence, exactly the same way you do it in the app delegate. But you are not in the app delegate, so you have to get there in steps:

  1. From the UIApplication class you can get a reference to the shared application object.

  2. From there you can get the app's delegate.

  3. From there you can get the app delegate's window.

  4. Now you can set the window's rootViewController.

The view controller that was the root view controller will vanish in a puff of smoke, along with all its dependent view controllers.

(This, however, is not quite how I would do it. I would have a stable root view controller whose only job is to be custom parent view controller to either the LoginViewController or the MainViewController. I'm not fond of the idea of changing root view controller's in the middle of the app. Still, it's not illegal.)



Related Topics



Leave a reply



Submit