在使用 Xcode 创建的 iOS Single View App 项目中,自动生成的 AppDelegate
有一个名为 window
的实例变量:
// In AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
var window: UIWindow?
// ...
}
但是,任何 iOS 应用程序的 UIApplication
单例也有一些与窗口相关的属性:
open class UIApplication : UIResponder {
// ...
open var keyWindow: UIWindow? { get }
open var windows: [UIWindow] { get }
// ...
}
我的猜测:
windows
只是一个长度为1的数组,其中唯一的窗口是AppDelegate
的窗口,并且keyWindows
也是AppDelegate
的窗口。
UIApplication
中的这是正确的吗? AppDelegate
的window
和UIApplication
的windows
有什么关系?
最佳答案
对于典型应用,您的猜测是正确的。
在某些情况下,应用可能会创建并显示额外的窗口。或者 iOS 可能会在使用某些功能时向应用程序添加额外的窗口。在这些情况下,windows
数组将有多个窗口(包括应用委托(delegate)引用的窗口),并且 keyWindow 在一段时间内可能是这些窗口中的任何一个(不一定是应用委托(delegate)的窗口)。
这是 UIApplicationDelegate 窗口
属性的文档:
This property contains the window used to present the app’s visual content on the device’s main screen.
Implementation of this property is required if your app’s
Info.plist
file contains theUIMainStoryboardFile
key. Fortunately, the Xcode project templates usually include a synthesized declaration of the property automatically for the app delegate. The default value of this synthesized property isnil
, which causes the app to create a genericUIWindow
object and assign it to the property. If you want to provide a custom window for your app, you must implement the getter method of this property and use it to create and return your custom window.
关于ios - UIApplication中的窗口和AppDelegate中的窗口是什么关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51028356/