在AppDelegate中,主UIWindow是如何实例化的? [英] In an AppDelegate, how is the main UIWindow instantiated?

查看:394
本文介绍了在AppDelegate中,主UIWindow是如何实例化的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Master-Detail Xcode项目中默认代码的片段

A snippet of the default code in a Master-Detail Xcode project

AppDelegate.m

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
 UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;  // *** here ***
    MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;
    return YES;
}

AppDelegate.h

AppDelegate.h

@property (strong, nonatomic) UIWindow *window;

我知道@synthesize只设置了访问器方法,并且没有自动执行初始化。但是, window 如果从未显式初始化,那么 rootViewController 如何?这只是Xcode在幕后启动吗?

I am aware that @synthesize just sets the accessor methods, and no initialization happens automagically. But how does window have a non-nil rootViewController if it is never explicitly initialized? Is this just Xcode init'ing behind the scenes?

推荐答案

来自我的书


如果在指定时选择了Storyboard选项模板,这个过程有点不同。该应用程序有一个主要的故事板,由Info.plist键主故事板文件基本名称( UIMainStoryboardFile )指向。在 UIApplicationMain 实例化app delegate类之后,它会向应用程序委托询问其窗口属性的值;如果该值为nil,则创建窗口并将其分配给app delegate的窗口属性。然后将故事板的初始视图控制器实例化并分配给窗口的 rootViewController 属性,结果是其视图作为其根视图放置在窗口中;然后在窗口中发送 makeKeyAndVisible 消息。所有这些都是在 UIApplicationMain 的幕后完成的,没有任何可见的代码。这就是为什么在故事板模板中,应用程序:didFinishLaunchingWithOptions:实现为空。

If you choose the Storyboard option as you specify a template, the process works a little differently. The app is given a main storyboard, pointed to by the Info.plist key "Main storyboard file base name" (UIMainStoryboardFile). After UIApplicationMain instantiates the app delegate class, it asks the app delegate for the value of its window property; if that value is nil, the window is created and assigned to the app delegate’s window property. The storyboard’s initial view controller is then instantiated and assigned to the window’s rootViewController property, with the result that its view is placed in the window as its root view; the window is then sent the makeKeyAndVisible message. All of that is done behind the scenes by UIApplicationMain, with no visible code whatever. That is why, in a storyboard template, the application:didFinishLaunchingWithOptions: implementation is empty.

这篇关于在AppDelegate中,主UIWindow是如何实例化的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆