Search code examples
iosstoryboardswift2seguexcode7.1

Going directly to Main View controller for already logged in user


I have a loginViewController as my rootviewcontroller followed by the main screen and then other screens. My views follow push and pop approach. What I want is, if a user is already logged in my view should start from main screen else start from login screen, and if I logout from main screen it should go back to login screen and the push and pop structure should be maintained. I can achieve this using the modal transition, but I need to use push and pop approach, is this possible? Currently I have checked a already logged in condition in my appdelegate to set the rootview controller but it fails if I attempt to log out as its not present in my navigation controller stack.

if !alreadyLoggedin
{
    let mainListVC = storyBoard.instantiateViewControllerWithIdentifier(“MainListViewController”)
    self.window!.rootViewController = mainListVC
}
else
{
    let loginVC = storyBoard.instantiateViewControllerWithIdentifier("ViewController")
    self.window!.rootViewController = loginVC
}

Solution

  • It's basic use case, Please follow my steps.

    1. In your story create navigation view controller with view controller.
    2. Select navigation view controller as your initial view controller in storyboard.
    3. Create three views controllers- InitialViewController, LoginViewController and MainViewController and set storyboard id for all view controllers.

    Created sample project for you. Download link (https://www.dropbox.com/s/zk8x7ptg5mzmotk/test.zip?dl=0)