Doug Smith
Doug Smith

Reputation: 29326

Why am I unable to change my initial view controller programmatically in my AppDelegate?

I'm following the advice of this question in order to define my initial view controller at runtime, instead of always being the same based off of the Storyboard property.

This is my didFinishLaunching method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UINavigationController *compactNavigationController = [mainStoryboard instantiateViewControllerWithIdentifier:@"CompactNavigationController"];
    self.window.rootViewController = compactNavigationController;
    [self.window makeKeyAndVisible];

    CompactPostsViewController *controller = (CompactPostsViewController *)compactNavigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;

    return YES;
}

And I've unchecked "Is Initial View Controller" in my Storyboard, and I've removed "Main" from the default Storyboard in my app's info.

But when I launch the app I get a completely black screen. What am I doing wrong?

Upvotes: 1

Views: 175

Answers (2)

Rameshwar Gupta
Rameshwar Gupta

Reputation: 791

check your linkings in your storyboard(by right click on the tableview controller).This happens mostly when you not linked your tableview controller properly.

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

Your window is nil because you never created it. You need to add this line,

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

The window is created for you when you have the "Main storyboard base name" entry in your info.plist, but if you remove that, then you have to create the window yourself.

Upvotes: 1

Related Questions