why is "window.rootViewController = self.navigationController" needed in Xcode 4 Navigation Based App?

Greg picture Greg · Apr 23, 2011 · Viewed 11.5k times · Source

Why is the following line needed within the didFinishLaunchingWithOptions method?

self.window.rootViewController = self.navigationController;

That is, noting there is already in Interface Builder, in the MainWindow XIB, the navigation controller with it's navigation bar and RootViewController within it's hierarchy.

Copy of whole method for reference is:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // Add the navigation controller's view to the window and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

Answer

Ole Begemann picture Ole Begemann · Apr 23, 2011

There is one thing you haven't yet done in MainWindow.xib: adding the nav controller's view to the window.

The line

self.window.rootViewController = self.navigationController;

does just that. The alternative (and what we wrote in iOS 3) is:

[self.window addSubview:self.navigationController.view];