IOS在运行时更改故事板默认视图控制器

它可以重写故事板的默认视图控制器来显示一个不同的控制器呢? 这当然会在AppDelegate中发生。

@ Martol1ni我想用你的答案,但是我也想远离不必要的故事板混乱,所以我调整了一下你的代码。 不过,我给了你一个令人鼓舞的答案+1。

我把所有以下的内容放在默认的控制器上。

 - (void)gotoScreen:(NSString *)theScreen { AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:theScreen]; [app.window setRootViewController:screen]; } 

然后在逻辑发生的地方,我会根据需要调用以下内容。

 if(myBool == YES) { [self gotoScreen:@"theIdentifier"]; } 

我肯定会在UINavigationController中embedded一个rootView,所以你没有两个,但三个视图。 从来没有启动,只是在控制所有其他人。 然后像这样实现其中的方法:

 - (void) decideViewController { NSString * result; if (myBool) { result = @"yourIdentifier"; } else { result = @"yourOtherIdentifier"; } self.navigationController.navigationBarHidden = YES; // Assuming you don't want a navigationbar UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:@"view1ident"]; [self.navigationController pushViewController:screen animated:NO]; // so it looks like it's the first view to get loaded } - (void)viewDidLoad { [super viewDidLoad]; [self decideViewController]; } 

它从不看起来像加载第一个视图。 如果你正在使用NIBS,你可以做一切从AppDelegate,但…