Daniel Tovesson Daniel Tovesson - 3 years ago 110
Swift Question

How to reset root view controller

Is it possible to reset the root view controller? With reset I mean resetting it to its initial state so

viewDidLoad
will be called again. I'm using a
UITabBarController
and when I logout I want all the tabs previously loaded to be unloaded.

Answer Source

You can do this by setting the instance of TabBarController to rootViewController on logout action.

Swift 3:

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyBoard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tabBarController
UIApplication.shared.keyWindow?.makeKeyAndVisible()

Objective C:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"TabBarController"];
[[[UIApplication sharedApplication] keyWindow] setRootViewController:tabBarController];
[[[UIApplication sharedApplication] keyWindow] makeKeyAndVisible];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download