问题
I am making an app. In which First User have to sign In then He allowed to use the app.
After successful login I want the user to directly switch to my HomeViewController.
Here is my code tho change navigation root view but it is not working
TermsConditionsController *firstViewController = [[TermsConditionsController alloc]init];
FirstPage *secondViewController = [[FirstPage alloc]init];
firstViewController.navigationController.viewControllers = [NSArray arrayWithObject: secondViewController];
FirstPage *nextScr = (FirstPage *) [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPage"];
[self.navigationController pushViewController:nextScr animated:YES];
回答1:
Connect your LoginViewController with HomeViewController by using segue.
give the identifier for the segue for ex name:successLogin.
and use this method:
[self performSegueIdentifier:@"successLogin" sender:nil];
回答2:
Try this code
Note: Change controller name as per your controllers
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController;
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"loginUserDetails"]) {
// Already logged in
ProfileViewController *profileVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ProfileVC"];
navController = [[UINavigationController alloc]initWithRootViewController:profileVC];
}else{
// Not Login Yet
LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginVC"];
navController = [[UINavigationController alloc]initWithRootViewController:loginVC];
}
self.window.rootViewController = navController;
}
// Write this code when user login successfully
if (/*Login Successfully*/) {
// Then add loginUserDetails in userdefaults.
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"loginUserDetails"];// Instead of nil pass here your object
[[NSUserDefaults standardUserDefaults] synchronize];
}
来源:https://stackoverflow.com/questions/30001518/issue-while-changing-navigation-root-view-controller