D
D
Dmitry Zamula2014-01-29 15:56:31
Objective-C
Dmitry Zamula, 2014-01-29 15:56:31

How to load the desired viewController in iOS?

When loading, I need the application to check what value is currently in UserDefaults in one key, and depending on the value, open different viewControllers. Simply put, if the user is already logged in, it opens the main screen, if not, then the authorization screen. Where and how to implement it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2014-01-29
@DimkaMind

In the case of the storyboard, I do the following:
- in the Deployment Info of the desired target, I remove the Main Interface ;
- add the following code to the application:didFinishLaunchingWithOptions: AppDelegate method

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

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil];
   
    UIViewController *firstViewControllerToShow = nil;
    
    if (yourCondition) {
        firstViewControllerToShow =
            [storyboard instantiateInitialViewController];
    } else {
        firstViewControllerToShow =
            [storyboard instantiateViewControllerWithIdentifier:@"YourAlternativeSceneStoryboardID"];
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = firstViewControllerToShow;
    [self.window makeKeyAndVisible];
    
    return YES;
}

where yourCondition is the condition depending on which one or another ViewController should be shown, for example, whether the user is registered or not. If registered, the ViewController that is specified as Initial in the storyboard will be shown. If not, a ViewController with an ID of YourAlternativeSceneStoryboardID .
Actually, without a storyboard, everything is the same, but without a storyboard. :)

K
Kirill Kunst, 2014-01-29
@leoru

If it's a storyboard, then my tactic is usually this:
1. Create an InitialController, assign it to initial in the storyboard.
2. In it, in viewDidLoad, I check if there is a user, if it is authorized and display the controller that I need:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self checkAuth];
}

- (void)checkAuth
{
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *navController;
  if (![AUUser currentUser]) {
        navController = [storyboard instantiateViewControllerWithIdentifier:@"authContainer"];
    } else {
        navController = [storyboard instantiateViewControllerWithIdentifier:@"mainViewContainer"];
    }
    
    navController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    navController.view.frame = self.view.bounds;
   
    [self addChildViewController:navController];
    [self.view addSubview:navController.view];
    self.currentController = navController;
}

K
Kirill Kunst, 2014-01-29
@leoru

Are you using a storyboard or not?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question