UIApplication.sharedApplication.delegate.window和UIApplication.sharedApplication.keyWindow有什么区别? [英] What is the difference between UIApplication.sharedApplication.delegate.window and UIApplication.sharedApplication.keyWindow?

查看:466
本文介绍了UIApplication.sharedApplication.delegate.window和UIApplication.sharedApplication.keyWindow有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我理解以下两行之间的区别:

Can anyone help me understand the difference between the following two lines:

[UIApplication.sharedApplication.delegate.window addSubview:myView];

[UIApplication.sharedApplication.keyWindow addSubview:myView];

推荐答案

在iOS上它们可能相同.当它们不同时,通常您将显示除应用程序委托的主窗口以外的另一个窗口.您的应用程序可以有许多窗口,但是只有keyWindow是在屏幕上可见并接收事件的窗口(例如,可以看到并接收事件的UIAlert是键窗口),引用:

They could be the same on iOS. When they are different, usually you are presenting another window other than the app delegate's main window. Your app can have many windows but only the keyWindow is the window that is visible on the screen and receiving events (example could be a UIAlert when visible and receiving events it is the keywindow) reference: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html

来自文档:

  • 对于UIApplication.sharedApplication.delegate.window:

演示情节提要时要使用的窗口.该属性包含 用于在设备的设备上显示应用程序视觉内容的窗口 主屏幕.

The window to use when presenting a storyboard. This property contains the window used to present the app’s visual content on the device’s main screen.

即这是您在AppDelegate.h文件中拥有的属性window.

i.e this is the property window that you have in your AppDelegate.h file.

  • 对于UIApplication.sharedApplication.keyWindow:

此属性将UIWindow对象保存在windows数组中,该数组是 最近发送了makeKeyAndVisible消息.

This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible message.

在iOS上,您可以在AppDelegate.m内部调用makeKeyAndVisible

On iOS you call makeKeyAndVisible in your AppDelegate.m inside

application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

您已将创建的appDelegate窗口设置为keyWindow.通常,银行应用程序在后台放置应用程序时会切换密钥窗口,以在双击主屏幕按钮时保护用户的敏感信息,而在前台运行时会切换回主代理窗口.

You made the created appDelegate window the keyWindow. Usually, bank apps switch the key window when the app is put in the background to protect the users sensitive information when the home button is doubled tapped and switch back to the main delegate window when the app is in foreground.

此答案与以下人员合作:@ SipkeSchoorstra,@ D-Mx和@andyDarwin

这篇关于UIApplication.sharedApplication.delegate.window和UIApplication.sharedApplication.keyWindow有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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