简体   繁体   English

设置UIWindow的rootViewController有什么作用?

[英]What does setting the UIWindow's rootViewController do?

Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller's view as the content view of the window. 将视图控制器分配给此属性(以编程方式或使用Interface Builder)将视图控制器的视图安装为窗口的内容视图。

The above quote is from the UIWindow's reference. 以上引用来自UIWindow的参考。 My question is about the particular phase : 我的问题是关于特定的阶段:

"installs the view controller's view as the content view of the window" “将视图控制器的视图安装为窗口的内容视图”

What does exactly content view refer to ? 确切的内容视图是指什么?

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html

Before the rootViewController property came along, most apps had code like this in the application delegate: rootViewController属性出现之前,大多数应用程序在应用程序委托中都有这样的代码:

[window addSubview:viewController.view];
[window makeKeyAndVisible];

This code set the view controller's view as the main view, but the UIWindow instance had no reference to the controller owning that view. 此代码将视图控制器的视图设置为主视图,但UIWindow实例没有引用拥有该视图的控制器。

When you use the rootViewController property, you don't need to add the view controller's view to the UIWindow instance anymore, this is done automatically. 当您使用rootViewController属性时,您不再需要将视图控制器的视图添加到UIWindow实例,这是自动完成的。 So the number of lines of code stays the same, but now your UIWindow has a reference to the view controller. 因此代码行数保持不变,但现在您的UIWindow具有对视图控制器的引用。

So, in newer applications, we now have code that looks like this: 因此,在较新的应用程序中,我们现在的代码如下所示:

window.rootViewController = viewController;
[window makeKeyAndVisible];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM