Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios change storyboard default view controller at run time

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.

like image 371
Jacksonkr Avatar asked May 15 '12 23:05

Jacksonkr


2 Answers

@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"];
}
like image 168
Jacksonkr Avatar answered Dec 08 '22 14:12

Jacksonkr


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...

like image 41
Martol1ni Avatar answered Dec 08 '22 14:12

Martol1ni