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

Jac*_*nkr 5 storyboard uiviewcontroller ios

是否可以从故事板覆盖默认视图控制器以显示不同的控制器?这当然会发生在AppDelegate中.

Jac*_*nkr 10

@ Martol1ni我想用你的答案,但我也想远离不必要的故事板杂乱,所以我稍微调整了你的代码.但是我确实给了你+1的鼓舞人心的答案.

我将以下所有内容都放在默认控制器上.

- (void)gotoScreen:(NSString *)theScreen
{
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:theScreen];
    [app.window setRootViewController:screen];
}
Run Code Online (Sandbox Code Playgroud)

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

if(myBool == YES) {
    [self gotoScreen:@"theIdentifier"];
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*1ni 5

我肯定会在UINavigationController中嵌入一个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];
}
Run Code Online (Sandbox Code Playgroud)

它看起来永远不会加载第一个视图.如果你正在使用NIBS,你可以从AppDelegate做任何事情......