1

I have a logout button and I need the app to reset. Seems pretty standard but I don't see much information on how I would go about doing something like this. So far from what I've picked up I've manage to remove an NSUserDefaults object that represents the current user, I use FBSDKLoginManager().logOut() in case the user is logged via Facebook, and then I reset the root view controller to its initial view controller:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = HomeViewController()

appDelegate.window!.rootViewController = rootViewController
appDelegate.window!.makeKeyAndVisible()

However, I'm not sure if there's anything else to do. I saw one answer that said you need to loop through all the windows subviews and remove them, but is this still necssary when you change the rootViewController? Is there anything else that I'm missing? Also, as of now the home screen just appears when you click logout. Is there any way to make changing the root view controller an animation?

Ryan Bobrowski
  • 2,641
  • 3
  • 31
  • 52
  • 2
    it depends on how your app is constructed, there is no single answer... – Wain Jun 04 '15 at 14:03
  • There is a HomeViewController that segues to login and registration modals. Once logged in, there is a container view controller that contains a view controller for the slide out side menu, and a navigation controller that contains the content. Content pages may or may not have subpages, but you can only access the menu to get to the logout button from the main (initial) content pages. That's pretty much it... – Ryan Bobrowski Jun 04 '15 at 14:04
  • You may be looking for an unwind segue back to your home screen. The accepted answer here: http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them does a beautiful job of explaining what they are and how to use them. – jrisberg Jun 04 '15 at 15:19

1 Answers1

6

Most of the time if you change the root view controller other view controllers will be deallocated if they are no longer in the heirarchy. To check you can implement deinit in the view controllers that should no longer be in memory.

deinit {
  println("SomeViewController was deallocated")
}

And you can try this for the transition animation

UIView.transitionWithView(self.window, duration: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {() -> Void in self.window.rootViewController = otherViewController}, completion: nil)
Max
  • 1,143
  • 7
  • 7