single function to dismiss all open view controlle

2019-01-13 20:48发布

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?

6条回答
欢心
2楼-- · 2019-01-13 20:54

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

查看更多
家丑人穷心不美
3楼-- · 2019-01-13 20:59

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.

查看更多
Fickle 薄情
4楼-- · 2019-01-13 21:01

You can call :

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

Should dismiss all view controllers above the root view controller.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-13 21:03

Swift3

navigationController?.popToRootViewControllerAnimated(true)
查看更多
疯言疯语
6楼-- · 2019-01-13 21:06

Updated answer for Swift 3.2 and swift 4

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

and when you use navigationController

self.navigationController?.popToRootViewController(animated: true)
查看更多
不美不萌又怎样
7楼-- · 2019-01-13 21:09

works for Swift 3.0 +

self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
查看更多
登录 后发表回答