66
votes

I have an app which is a single view application. I have a navigation controller linked up to all child controllers from the root view controller.

In each child controller, I have a logout button. I'm wondering if I can have a single function I can call which will dismiss all the controllers which have been open along along the way, no matter which controller was currently open when the user presses logout?

My basic start:

func tryLogout(){
     self.dismissViewControllerAnimated(true, completion: nil)
     let navigationController = UINavigationController(rootViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoginViewController") )
     self.presentViewController(navigationController, animated: true, completion: nil)
}

I am looking for the most memory efficient way of carrying out this task. I will put my logout function in a separate utils file, but then I can't use self. And I still have the issue of knowing which controllers to dismiss dynamically.

Update Pop to root view controller has been suggested. So my attempt is something like:

func tryLogout(ViewController : UIViewController){
     print("do something")
     dispatch_async(dispatch_get_main_queue(), {
         ViewController.navigationController?.popToRootViewControllerAnimated(true)
         return
     })
 }

Would this be the best way to achieve what I'm after?

9
You might just want to reinitialize your app's UIWindow's rootViewController property. That will remove your entire view hierarchy (assuming you're not using other windows, which is rare).Aaron Brager
Are you presenting controllers or just pushing onto the nav stack? Where is the logout button? Can you just pop to root? What other clean up do you need to do?Wain
@Wain I have my controller linked up with segue identifiers in certain cases and I am presentingViewControllers with the defined identifier. Then there are some cases where I'm just doing basic segues i.e. a button's action, a table row's click event. The logout button is added to the nav bar as a left bar button item. No other clean up really. Reset a few Global Prefs justuser2363025
@Aaron Brager sorry if this is a silly question, but what do you mean exactly about using other windows?user2363025
@user2363025 Very rarely an iOS app will have multiple UIWindows. You would know if you were making and managing them.Aaron Brager

9 Answers

199
votes

You can call :

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

Should dismiss all view controllers above the root view controller.

41
votes

Updated answer for Swift 4 and swift 5

UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)

and when you use navigationController

self.navigationController?.popToRootViewController(animated: true)
31
votes

Swift 4:

To dismiss any unwanted residue Modal ViewControllers, I used this and worked well without retaining any navigation stack references.

UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: nil)

self.view.window! did crash for my case possibly because its a Modal screen and had lost the reference to the window.

7
votes

Swift3

navigationController?.popToRootViewControllerAnimated(true)
6
votes

Take a look at how unwind segues work. Its super simple, and lets you dismiss/pop to a certain viewcontroller in the heirarchy, even if it consists of a complex navigation (nested pushed and or presented view controllers), without much code.

Here's a very good answer (by smilebot) that shows how to use unwind segues to solve your problem https://stackoverflow.com/a/27463286/503527

4
votes

To dismiss all modal Views.

Swift 5


view.window?.rootViewController?.dismiss(animated: true, completion: nil)

0
votes

works for Swift 3.0 +

self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
0
votes

If you have a customed UITabbarController, then try dismiss top viewController in UITabbarController by:

class MainTabBarController: UITabBarController {

    func aFuncLikeLogout() {

        self.presentedViewController?.dismiss(animated: false, completion: nil)

        //......
    }

}
-1
votes

Swift Used this to jump directly on your ROOT Navigation controller.

self.navigationController?.popToRootViewController(animated: true)