在AppDelegate中,主UIWindow是如何实例化的? [英] In an AppDelegate, how is the main UIWindow instantiated?
问题描述
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
如果从未显式初始化,那么
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
). AfterUIApplicationMain
instantiates the app delegate class, it asks the app delegate for the value of itswindow
property; if that value is nil, the window is created and assigned to the app delegate’swindow
property. The storyboard’s initial view controller is then instantiated and assigned to the window’srootViewController
property, with the result that its view is placed in the window as its root view; the window is then sent themakeKeyAndVisible
message. All of that is done behind the scenes byUIApplicationMain
, with no visible code whatever. That is why, in a storyboard template, theapplication:didFinishLaunchingWithOptions:
implementation is empty.
这篇关于在AppDelegate中,主UIWindow是如何实例化的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!