shane
shane

Reputation: 1863

Instantiate view controller from within navigation stack on launch

I have an app like this

Navigation controller -> login controller -> main view controller -> other stuff

I want to try to login in applicationdidfinishloadingwithoptions, and if that is successful then load main view controller, otherwise load the login view controller. My problem is I want the navigation stack to remain intact like above no matter what, so that I can pop back to my login view controller if I want to log out.

Right now I try to instantiate a main view controller on successful login, but on logout and other navigations it complains that I don't have a navigation controller embedded.

What is the correct way to do this?

Upvotes: 1

Views: 1215

Answers (3)

raja
raja

Reputation: 1161

Navigation controller -> login controller -> main view controller -> other stuff

Change your Navigation Stack,

Navigation controller - > main view controller -> other stuff

So main view controller is the RootViewController of the navigation stack.

if(userloginstatus == YES) {

Do other stuff, show other screens based on the flow.

} else {

Push or present 'loginviewcontroller' from the main view controller, So after successfull login just pop the 'loginviewcontroller'

}

Upvotes: 0

shane
shane

Reputation: 1863

Yogesh's answer worked well for me, however I found a more elegant way to carry out exactly what I needed. In applicationdidfinishlaunchingwithoptions I perform a test to log in. If I am logged in (test successful) I do push an instance of my main view controller onto the stack like this.

if(loggedIn) {
    UIViewController *main = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Main"];
    [(UINavigationController *)self.window.rootViewController pushViewController:main animated:NO];

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

If you are using storyboards then first of all create a UIViewController for Login and give it a storyboard ID, second create your mainViewController and embed it in UINavigationController and give storyboard id to UINavigationController.

After that in AppDelegate.m's applicationdidfinishloadingwithoptions load your appropriate VC based on user is logged in OR not.

Example

// Check if user is logged in
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"loggedIn"] == NULL || [[[NSUserDefaults standardUserDefaults] stringForKey:@"loggedIn"] isEqualToString:@"false"]) {
        // show login page
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        UIViewController *mainViewController = [storyboard instantiateViewControllerWithIdentifier:@"login"];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = mainViewController;
        [self.window makeKeyAndVisible];
} else {

        // show home page
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *mainViewController = [storyboard instantiateViewControllerWithIdentifier:@"home"];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = mainViewController;
        [self.window makeKeyAndVisible];
}

Edit

So your stack will become like this

NavController->mainVC->OtherStuff

And standalone

LoginVC

Upvotes: 1

Related Questions