4

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;
}
Greg
  • 34,042
  • 79
  • 253
  • 454
  • If you make the right attachments in MainWindow.xib, from within IB, you can safely remove that line of code. – m4rkk Oct 23 '11 at 15:51

1 Answers1

7

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];
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • thanks Ole - however do you understand why this isn't accomplished in IB? Is there a reason why IB got a lot of the other linkages in place but not this one? I'm not sure whether there is a good reason for this or whether it is an IB feature that Apple could implement in the future? – Greg Apr 23 '11 at 11:52
  • Well, IB just lacks the functionality to add a view controller's view to another view. I agree that it would be nice to have and make IB easier to understand. At the same time, I would love to have the ability to add a view to another view when the two views are defined in different NIB files. – Ole Begemann Apr 23 '11 at 11:55
  • 2
    One thing to note, self.window.rootViewController is *NOT* an instance of RootViewController, nor is it the IBOutlet to RootViewController that you see in IB (that would be self.rootViewController). This confused me too for a bit until I noticed the difference. – DOOManiac Apr 23 '11 at 17:55
  • Thanx a lot @OleBegemann after a lot of head storm I found the solution working for me.. you saved me friend. Thanx :) +1 – iOS Monster Jun 21 '12 at 10:51