更改导航根视图控制器时出现问题

时间:2015-05-02 11:05:03

标签: ios objective-c xcode uinavigationcontroller xcode6

我正在制作一款应用。首先用户必须登录,然后他才允许使用该应用程序。

成功登录后,我希望用户直接切换到我的HomeViewController。

这是我的代码更改导航根视图但它无法正常工作

   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];

enter image description here

1 个答案:

答案 0 :(得分:0)

试试此代码

注意:根据您的控制器更改控制器名称

- (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];
}