ios change storyboard default view controller at run time

谁都会走 提交于 2020-01-12 08:27:10

问题


It it possible to override the default view controller from a storyboard to show a different controller instead? This would all happen in the AppDelegate of course.


回答1:


@Martol1ni I wanted to use your answer, but I also wanted to stay away from unnecessary storyboard clutter so I tweaked your code a bit. However I did give you a +1 for your inspiring answer.

I put all of the following on the default controller.

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

    UIViewController *screen = [self.storyboard instantiateViewControllerWithIdentifier:theScreen];
    [app.window setRootViewController:screen];
}

And then where the logic happens I'll call the following as needed.

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



回答2:


I would definately embed a rootView in a UINavigationController, so you have not two, but three views. The one is never launched, just in control of all the otherones. Then implement the methods in it like this:

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

It never looks like the first view is loaded. If you are using NIBS, you can do everything from AppDelegate though...



来源:https://stackoverflow.com/questions/10610012/ios-change-storyboard-default-view-controller-at-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!