如何更改rootviewcontroller

我想在authenticationViewController之后更改rootViewController 在此处输入图像描述

-(IBAction)LoginButtonPushed:(id)sender { if ([(VerifyId)isEqual:@"C"]){ CondidatsViewController *condidatsViewController = [[[CondidatsViewController alloc]initWithNibName:@"CondidatsViewController" bundle:nil]autorelease]; UINavigationController *navController = self.navigationController; NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease]; [controllers removeLastObject]; navController.viewControllers = controllers; [navController pushViewController:condidatsViewController animated: YES]; } else { RecruteursViewController *recruteursViewController = [[[RecruteursViewController alloc]initWithNibName:@"RecruteursViewController" bundle:nil]autorelease]; UINavigationController *navController = self.navigationController; NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease]; [controllers removeLastObject]; navController.viewControllers = controllers; [navController pushViewController:recruteursViewController animated: YES]; } } 

这段代码是当我按下登录按钮时我想要CondidatsViewController或RecruteursViewController将是rootView

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. AcceuilViewController *viewController =[[[AcceuilViewController alloc] initWithNibName:@"AcceuilViewController" bundle:nil]autorelease]; self.navController = [[[UINavigationController alloc]initWithRootViewController:viewController] autorelease]; self.window.rootViewController = self.navController; [self.window makeKeyAndVisible]; return YES; } 

您可以尝试UINavigationController的以下方法,使用新的所需视图控制器数组

  [self.navigationController setViewControllers:@[newRootViewControllerInstance, secondVCInstanceIfRequired, thirdVC-and-so-on....] animated:NO]; 

它会做的伎俩;)

将视图控制器放在导航和身份validation视图控制器上一层怎么样? 此视图控制器可以检查有效会话并在堆栈顶部推送适当的视图。 尝试更改根视图控制器似乎是不明智的。

编辑:更多细节

你有一个根视图控制器没有像大多数其他视图控制器可能绑定的视图。 您将此类设置为应用程序委托中的根视图控制器。

App Delegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. RootViewController *rootViewController = [[RootViewController alloc] init]; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; return YES; } 

在根视图控制器中,您可以确定是否具有有效会话。 基于此,您可以渲染适当的视图。

RootViewController.m

 @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; BOOL isUserAuthenticated = [MyClasskToFigureOutIfUserIsAuthenticated isUserAuthenticated]; if(isUserAuthenticated == NO) { AuthViewController *vcAuth = [[AuthViewController alloc] init]; [self addChildViewController:vcAuth]; [self.view addSubView:vcAuth.view]; } else { //they are authenticated so push your other view controller. } } @end 

这非常粗糙,但它应该指向正确的方向。