Answer the question
In order to leave comments, you need to log in
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
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;
}
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question