Reputation: 2590
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.
Upvotes: 4
Views: 6284
Reputation: 2829
Set view
property of UIViewController
to nil
UIApplication.shared.keyWindow?.rootViewController?.view = nil
it will force UIViewController
to init his life cycle from a beginning after next call to self.view
Upvotes: 0
Reputation: 3157
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];
Upvotes: 6
Reputation: 14329
If you are using navigation controller on Tabbarcontroller
then navigate to that navigation controller otherwise go to Tabbarcontroller
as-
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBar = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! TabBarController
appDelegate.window?.rootViewController = tabBar
appDelegate.window?.makeKeyAndVisible()
Upvotes: 1