title: single function to dismiss all open view controllers
body: <p>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.</p>
  
  <p>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?</p>
  
  <p>My basic start:</p>
  
  <pre><code>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)
  }
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Update</strong>
  Pop to root view controller has been suggested. So my attempt is something like:</p>
  
  <pre><code>func tryLogout(ViewController : UIViewController){
       print("do something")
       dispatch_async(dispatch_get_main_queue(), {
           ViewController.navigationController?.popToRootViewControllerAnimated(true)
           return
       })
   }
  </code></pre>
  
  <p>Would this be the best way to achieve what I'm after?</p>