为什么Xcode 4基于导航的应用程序需要"window.rootViewController = self.navigationController"?

Gre*_*reg 4 iphone uitableview uinavigationcontroller ios rootview

为什么didFinishLaunchingWithOptions方法中需要以下行?

self.window.rootViewController = self.navigationController;
Run Code Online (Sandbox Code Playgroud)

也就是说,在Interface Builder中,MainWindow XIB中已经有导航控制器,它的导航栏和RootViewController在它的层次结构中.

整个方法的副本供参考:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

Ole*_*ann 7

在MainWindow.xib中还有一件事你没做:将导航控制器的视图添加到窗口.

这条线

self.window.rootViewController = self.navigationController;
Run Code Online (Sandbox Code Playgroud)

做到了那一点.替代方案(以及我们在iOS 3中编写的内容)是:

[self.window addSubview:self.navigationController.view];
Run Code Online (Sandbox Code Playgroud)

  • 有一点需要注意,self.window.rootViewController是*NOT*RootViewController的一个实例,也不是你在IB中看到的RootViewController的IBOutlet(也就是self.rootViewController).在我注意到差异之前,这让我有点困惑. (2认同)