user2461593
user2461593

Reputation: 87

How to always load into same UIViewController

I have an app with several views but I want to make sure it always loads into the same view.

Basically the app is password protected and I need to always make sure it loads into the password view.

How can I go about doing this.

Thanks

Upvotes: 0

Views: 83

Answers (2)

Groot
Groot

Reputation: 14251

If you always want to the application to launch into the same UIViewController you need to set the RootViewController of your application. You can do this in the Interface Builder by ticking the box that says "Is Initial View Controller"

enter image description here

or in your (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method you can set the rootViewController as

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    //...
    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MyLoginViewControllerStoryboardID"]; 

    return YES;
}

Note that you then have to set the storyboard ID properly in the Interface Builder. That is

enter image description here

Also, on a side note, if you want to show the LoginViewController only if the user is currently not logged in you can do something a like this

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

    BOOL isLoggedIn = [[NSUserDefaults standardDefaults] boolForKey:@"IsLoggedIn"];

    NSString *rootStoryboardID;
    if(isLoggedIn) {
        rootStoryboardID = @"LoginViewControllerStoryboardID";
    } else {
        rootStoryboardID = @"MainViewControllerStoryboardID";
    }

    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:rootStoryboardID]; 

    return YES;
}

Hope it helps!

Upvotes: 2

Alyssa Ross
Alyssa Ross

Reputation: 2169

Assuming you're using storyboards:

  1. Select the view controller you would like to load first in your storyboard.
  2. Make sure the utilities pane is showing (Command-Option-0)
  3. Make sure the attributes inspector is displayed: (Command-Option-4)
  4. Check the box labeled "Is Initial View Controller"

Upvotes: 0

Related Questions