ios - UIApplication中的窗口和AppDelegate中的窗口是什么关系?

标签 ios cocoa-touch appdelegate

在使用 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 }
  // ...
}

我的猜测:

    UIApplication中的
  • windows只是一个长度为1的数组,其中唯一的窗口是AppDelegate的窗口,并且
  • keyWindows 也是 AppDelegate 的窗口。

这是正确的吗? AppDelegatewindowUIApplicationwindows有什么关系?

最佳答案

对于典型应用,您的猜测是正确的。

在某些情况下,应用可能会创建并显示额外的窗口。或者 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 the UIMainStoryboardFile 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 is nil, which causes the app to create a generic UIWindow 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/

相关文章:

ios - 推送 View Controller ,但返回另一个

ios - 模态视图在关闭时更改我的选项卡

ios - 在自定义 UIView 和 UIViewController 中同时使用相同的 UITextFieldDelegate 方法

iphone - 如何将 iPhone 联系人拉入 UIWebView?

android - 适用的免费版本是否支持 adMob 或 ChartBoost 广告?

iphone - 在 iPhone 上阅读 Cacao(不是 Cocoa Touch)文件

ios - UIGesturerecognizer 不调用 shouldRecognizeSimultaneouslyWithGestureRecognizer

iphone - 为什么我必须最后调用 super -dealloc,而不是首先调用?

ios - xCode 中的 Firebase.configure() 导致线程 1 : signal SIGABRT : terminating with uncaught exception of type NSException on App Delegate