5

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.

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • 1
    The default ViewController is declared in info.pList in the latest SDK's. Is it supposed to check for something at startup, and then decide what viewcontroller to push? – Martol1ni May 15 '12 at 23:19
  • @Martol1ni yes, what you said is exactly what I'm looking for. – Jacksonkr May 15 '12 at 23:43
  • Are you using a UINavigationController ? – Martol1ni May 15 '12 at 23:55
  • No, only `UIViewController`s. I have two of them and one is the default. On `myBool == YES` I want to open the app up with my second controller instead of the default one. – Jacksonkr May 16 '12 at 00:04

2 Answers2

10

@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"];
}
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
5

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

Martol1ni
  • 4,684
  • 2
  • 29
  • 39