找到你要的答案

Q:issue while changing Navigation Root View Controller

Q:更改导航根视图控制器时的问题

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

我正在制作一个应用程序。在第一个用户必须登录,然后他允许使用的应用程序。

登录成功后我想让用户直接切换到我的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];

answer1: 回答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]; 

连接你的loginviewcontroller用SEGUE homeviewcontroller。

给前名的各种标识符:successlogin。

并使用此方法:

[self performSegueIdentifier:@"successLogin" sender:nil]; 
answer2: 回答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];
}

试试这个代码

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

- (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];
}
ios  objective-c  xcode  uinavigationcontroller  xcode6