issue while changing Navigation Root View Controller

巧了我就是萌 提交于 2019-12-24 13:15:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!