2

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
}
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Francis F
  • 3,157
  • 3
  • 41
  • 79

2 Answers2

1

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)

Vignesh Kumar
  • 598
  • 4
  • 11
  • Yes, this would work but I need to maintain my push and pop animation as well. So if I enable animation I would see initialviewcontroller appearing in between. – Francis F Feb 26 '16 at 10:56
  • Try to download it [Example] (https://www.dropbox.com/s/zk8x7ptg5mzmotk/test.zip?dl=0).. i have added the animation. – Vignesh Kumar Feb 26 '16 at 13:55
  • What's your code for logout? Is it possible that the login view (and all data/credentials) are going through deinit (so lost)? If you put a print statement in deinit you should be able to confirm view's status. In theory, you could use dismissViewController to return to your login screen... – zevij Feb 27 '16 at 22:44
  • No need of dismissViewController to return login screen. Bcz I have used Show segue type(PUSH) to navigate from login to main view controller. I used for logout.. self.navigationController?.popToRootViewControllerAnimated(true) – Vignesh Kumar Feb 29 '16 at 04:54
0

You need to use. AppDelegate

Objective C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LoginStatus"]) {
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"loginController"];
    }else{
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"RootView"];

    }

}

SWIFT - i hope its correct

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

      //Using UserDefaults check already loggedin user or not
        if (!someStatus) {

            let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
            self.presentViewController(vc, animated: true, completion: nil)

        }else{


            let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! UIViewController
            self.presentViewController(vc, animated: true, completion: nil)

        }

    }
Im Batman
  • 1,842
  • 1
  • 31
  • 48