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.

4
  • 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
    Commented May 15, 2012 at 23:19
  • @Martol1ni yes, what you said is exactly what I'm looking for.
    – Jacksonkr
    Commented May 15, 2012 at 23:43
  • Are you using a UINavigationController ?
    – Martol1ni
    Commented May 15, 2012 at 23:55
  • No, only UIViewControllers. 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
    Commented May 16, 2012 at 0:04

2 Answers 2

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"];
}
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...

Not the answer you're looking for? Browse other questions tagged or ask your own question.