ios UIApplication.sharedApplication.delegate.window 和 UIApplication.sharedApplication.keyWindow 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21698482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:25:02  来源:igfitidea点击:

What is the difference between UIApplication.sharedApplication.delegate.window and UIApplication.sharedApplication.keyWindow?

iosiphoneobjective-c

提问by tv.ashvin

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

谁能帮我理解以下两行之间的区别:

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

采纳答案by Basheer_CAD

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 keyWindowis 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

它们在 iOS 上可能相同。当它们不同时,通常您会呈现除应用程序委托的主窗口之外的另一个窗口。您的应用程序可以有多个窗口,但只有在keyWindow屏幕上可见并接收事件的窗口(示例可能是 UIAlert,当可见和接收事件时它是 keywindow)参考:https: //developer.apple.com/library /content/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html

from the documentation:

从文档:

  • For UIApplication.sharedApplication.delegate.window:
  • 对于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.

呈现故事板时使用的窗口。此属性包含用于在设备主屏幕上呈现应用程序视觉内容的窗口。

i.e this is the property windowthat you have in your AppDelegate.hfile.

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

  • For UIApplication.sharedApplication.keyWindow:
  • 对于UIApplication.sharedApplication.keyWindow

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

此属性将 UIWindow 对象保存在最近发送 makeKeyAndVisible 消息的 windows 数组中。

On iOS you call makeKeyAndVisiblein your AppDelegate.minside

在 iOS 上,你呼唤makeKeyAndVisible你的AppDelegate.m内心

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

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

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.

您将创建的 appDelegate 窗口设为 keyWindow。通常,银行应用程序在应用程序置于后台时切换关键窗口以保护用户在双击主页按钮时的敏感信息,并在应用程序处于前台时切换回主代理窗口。

This answer is in collaboration with: @SipkeSchoorstra, @D-Mx and @andyDarwin

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

回答by Jeffery Thomas

For most uses, they will be the same… but not always.

对于大多数用途,它们将是相同的……但并非总是如此。

[UIApplication sharedApplication].keyWindowis the window which is currently being displayed on the device. This is normally your application's window, but could be a system window.

[UIApplication sharedApplication].keyWindow是当前显示在设备上的窗口。这通常是您应用程序的窗口,但也可能是系统窗口。

[UIApplication sharedApplication].delegate.windowis the window your application is expected to use.

[UIApplication sharedApplication].delegate.window是您的应用程序预期使用的窗口。

Which one should be used? Well that all depends on context.

应该使用哪一种?嗯,这一切都取决于上下文。

If you are updating part of your application, then you should add views to your application's window. This is almost always what you want to do.

如果您正在更新应用程序的一部分,那么您应该向应用程序的窗口添加视图。这几乎总是你想要做的。

Personally, I always use [[UIApplication sharedApplication].delegate.window addSubview:view]or [self.view.window addSubView:view](within UIViewController) when I need to add a view directly to the window.

就个人而言,当我需要将视图直接添加到窗口时,我总是使用[[UIApplication sharedApplication].delegate.window addSubview:view][self.view.window addSubView:view](在 内UIViewController)。

There might be some times when you want to present a view to the window currently being displayed, regardless if the window belongs to your application or is some system window. I've not run into that situation.

有时您可能希望向当前显示的窗口呈现视图,无论该窗口属于您的应用程序还是某个系统窗口。我没有遇到过这种情况。

回答by Andy Darwin

Basheer_CAD's answer is not right. They are not always the same in iOS.

Basheer_CAD 的回答不对。它们在 iOS 中并不总是相同的。

Jeffery Thomas's answer is right and let me provide a concrete example.

Jeffery Thomas 的回答是正确的,让我提供一个具体的例子。

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"keyWindow --------> %@",[UIApplication sharedApplication].keyWindow.rootViewController);
    NSLog(@"delegate.window --> %@",[UIApplication sharedApplication].delegate.window.rootViewController);
    NSLog(@"self.view.window -> %@",self.view.window.rootViewController);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
    [alert show];
    NSLog(@"keyWindow --------> %@",[UIApplication sharedApplication].keyWindow.rootViewController);
    NSLog(@"delegate.window --> %@",[UIApplication sharedApplication].delegate.window.rootViewController);
    NSLog(@"self.view.window -> %@",self.view.window.rootViewController);
}

The output is:

输出是:

keyWindow --------> (null)
delegate.window --> <ViewController: 0x10030c0e0>
self.view.window -> (null)
keyWindow --------> <UIApplicationRotationFollowingController: 0x100204510>
delegate.window --> <ViewController: 0x10030c0e0>
self.view.window -> <ViewController: 0x10030c0e0>

When viewDidLoad, actually the window is not ready yet, so there is nothing for the system window. UIAlertViewmay dominate the window, so you cannot get the window you want.

viewDidLoad,实际上窗口还没有准备好,所以系统窗口没有任何东西。UIAlertView可能会支配窗口,因此您无法获得所需的窗口。

回答by plu

The simplest setup is to just have one UIWindow. Usually that window is kept as a property on your app delegate. The keyWindowis the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window. So if you add a 2nd window and make it the keyWindow(via [window makeKeyAndVisible]), your lines return different windows!

最简单的设置是只有一个UIWindow. 通常,该窗口作为应用程序委托的属性保留。该keyWindow是指定接收键盘和其他非触摸相关的事件之一。一次只有一个窗口可能是关键窗口。因此,如果您添加第二个窗口并将其设为keyWindow(via [window makeKeyAndVisible]),您的行将返回不同的窗口!