Search code examples
iosswiftstoryboard

Handle Navigation Controller for Log Out


This is the Navigation Stack of my App. New User

  • Splash -> LoginVIewController -> TabBarController (With Navigation Controller on Each Tab) -> Tab(3) ProfileViewController -> User Clicks on Logout

Existing User (Already Logged in user)

  • Splash -> TabBarController (With Navigation Controller on Each Tab) -> Tab(3) ProfileViewController -> User Clicks on Logout

How should I create a segue so that there is no memory leak? When the user clicks on the LogOut Button. It should create a new instance of LoginViewController and all the allocated memory of TabbarController should be removed.


Solution

  • You can create a new object of LoginVIewController and set it as a rootViewController.

       let story = UIStoryboard(name: "Main", bundle:nil)
       let vc = story.instantiateViewController(withIdentifier: "ViewController") as! ViewController
       UIApplication.shared.windows.first?.rootViewController = vc
       UIApplication.shared.windows.first?.makeKeyAndVisible()