Instantiate view controller from within navigation stack on launch

shane

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?

Yogesh Suthar

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Presenting a view controller modally within a navigation stack

From Dev

How to instantiate a navigation controller from another view controller?

From Dev

iOS: remove view controller from navigation stack

From Dev

How to instantiate a navigation and tab controller from another view controller which is in segue(show detail)?

From Dev

Dismiss navigation controller and all of its view controllers stack in UIPopupview using a button within one of the views

From Dev

Dismiss navigation controller and all of its view controllers stack in UIPopupview using a button within one of the views

From Dev

Moving View Controller to the top of navigation stack iOS

From Dev

Skip/Add a View Controller in Navigation Stack

From Dev

Redirect to new view controller in the middle of navigation stack

From Dev

Keep View Controller Out of Navigation Stack

From Dev

Method of UINavigationController to add a view controller to navigation stack?

From Dev

How to launch View Controller from a custom UICollectionViewCell

From Dev

whats the correct way to be back to top view controller in the navigation controller stack?

From Java

Instead of push segue how to replace view controller (or remove from navigation stack)?

From Dev

Remove view controller from navigation stack after push segue (using Storyboard segues)

From Dev

Moving from Navigation controller to parent view controller

From Dev

move back to View Controller from Navigation Controller?

From Dev

Instantiate View Controller not working

From Dev

Instantiate View Controller not working

From Dev

Pushing a view onto the Navigation Stack from a view inside a container view?

From Java

How to check if a view controller is presented modally or pushed on a navigation stack?

From Dev

viewDidAppear() called before view controller is pushed onto navigation stack

From Dev

Navigate from a navigation bar view controller to a tab bar view controller

From Dev

Navigate from a navigation bar view controller to a tab bar view controller

From Dev

Swift: Is it possible to remove View Controller from stack?

From Dev

Instantiate View Controller from Storyboard vs. Creating New Instance

From Dev

Instantiate a view controller from UIStoryBoard after using TyphoonAssemblyActivator

From Dev

Instantiate a view controller from UIStoryBoard after using TyphoonAssemblyActivator

From Dev

Navigation From One View Controller to Another

Related Related

  1. 1

    Presenting a view controller modally within a navigation stack

  2. 2

    How to instantiate a navigation controller from another view controller?

  3. 3

    iOS: remove view controller from navigation stack

  4. 4

    How to instantiate a navigation and tab controller from another view controller which is in segue(show detail)?

  5. 5

    Dismiss navigation controller and all of its view controllers stack in UIPopupview using a button within one of the views

  6. 6

    Dismiss navigation controller and all of its view controllers stack in UIPopupview using a button within one of the views

  7. 7

    Moving View Controller to the top of navigation stack iOS

  8. 8

    Skip/Add a View Controller in Navigation Stack

  9. 9

    Redirect to new view controller in the middle of navigation stack

  10. 10

    Keep View Controller Out of Navigation Stack

  11. 11

    Method of UINavigationController to add a view controller to navigation stack?

  12. 12

    How to launch View Controller from a custom UICollectionViewCell

  13. 13

    whats the correct way to be back to top view controller in the navigation controller stack?

  14. 14

    Instead of push segue how to replace view controller (or remove from navigation stack)?

  15. 15

    Remove view controller from navigation stack after push segue (using Storyboard segues)

  16. 16

    Moving from Navigation controller to parent view controller

  17. 17

    move back to View Controller from Navigation Controller?

  18. 18

    Instantiate View Controller not working

  19. 19

    Instantiate View Controller not working

  20. 20

    Pushing a view onto the Navigation Stack from a view inside a container view?

  21. 21

    How to check if a view controller is presented modally or pushed on a navigation stack?

  22. 22

    viewDidAppear() called before view controller is pushed onto navigation stack

  23. 23

    Navigate from a navigation bar view controller to a tab bar view controller

  24. 24

    Navigate from a navigation bar view controller to a tab bar view controller

  25. 25

    Swift: Is it possible to remove View Controller from stack?

  26. 26

    Instantiate View Controller from Storyboard vs. Creating New Instance

  27. 27

    Instantiate a view controller from UIStoryBoard after using TyphoonAssemblyActivator

  28. 28

    Instantiate a view controller from UIStoryBoard after using TyphoonAssemblyActivator

  29. 29

    Navigation From One View Controller to Another

HotTag

Archive