ios 如何在 swift 应用程序中获取 keyWindow 引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24215462/
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
How do I get the keyWindow reference in a swift app?
提问by zumzum
In Objective-C in the viewDidLoad method of a UIViewController I do this to get the keyWindow reference in my iOS app:
在 UIViewController 的 viewDidLoad 方法中的 Objective-C 中,我这样做是为了在我的 iOS 应用程序中获取 keyWindow 引用:
UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
So, I am porting this view controller into Swift and I do this one the viewDidLoad call:
因此,我将此视图控制器移植到 Swift 中,并通过 viewDidLoad 调用执行此操作:
let window = UIApplication.sharedApplication().keyWindow
I am trying to understand why the window is nil. When I put a breakpoint right after that line I then inspect window in the console and I get this:
我试图理解为什么窗口为零。当我在该行之后放置一个断点时,我会检查控制台中的窗口,然后得到以下信息:
(lldb) po window
nil
How can I get window to be a valid reference to the keyWindow of my application in swift?
如何快速使 window 成为对我的应用程序的 keyWindow 的有效引用?
回答by superarts.org
I came to this question when I was searching for getting window in swift. If you want to get window instead of keyWindow, try this:
当我在 swift 中搜索获取窗口时,我遇到了这个问题。如果你想得到 window 而不是 keyWindow,试试这个:
if let app = UIApplication.sharedApplication().delegate as? AppDelegate, let window = app.window {
MBProgressHUD.show(text, view:window)
}
Updated for Swift 3: (Thanks @Trevor)
为 Swift 3 更新:(感谢 @Trevor)
if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
MBProgressHUD.show(text, view:window)
}
回答by juhan_h
Swift 4 simply has UIApplication.shared.keyWindow
property, no casting necessary.
Swift 4 只是有UIApplication.shared.keyWindow
属性,不需要强制转换。
Note that iOS 13/iPadOS introduces UIScenes
and explicit support for multiple windows, and thus deprecates the concept of keyWindow
as it is no longer valid.
请注意,iOS 13/iPadOS 引入UIScenes
并明确支持多个窗口,因此弃用了keyWindow
不再有效的概念。
Thisquestion has an overview how to get access to scene based windows.
这个问题概述了如何访问基于场景的窗口。
回答by rmooney
Updating superarts.org's answer for Swift 3:
更新 superarts.org 对 Swift 3 的回答:
if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
MBProgressHUD.show(text, view: window)
}
回答by rdelmar
The key window must not yet be set at the time of viewDidLoad. Your code works fine in viewDidAppear. Alternately, you can get the windows array in viewDidLoad, and get one (if there's more than one) of the windows from that array.
在 viewDidLoad 时必须尚未设置关键窗口。您的代码在 viewDidAppear 中运行良好。或者,您可以在 viewDidLoad 中获取 windows 数组,并从该数组中获取一个(如果有多个)windows。
回答by Irshad Ahmed
Don't call window in didLoad function call the keyWindow in viewDidAppear function of ViewController
不要在 didLoad 函数中调用 window 调用 ViewController 的 viewDidAppear 函数中的 keyWindow
回答by WILL K.
The easiest way to do that is:
最简单的方法是:
in Objective-C
在 Objective-C 中
[UIApplication sharedApplication].windows.firstObject
in Swift
在斯威夫特
UIApplication.shared.windows.first!
Note that this works only if you do not support multiple windows.
请注意,这仅在您不支持多个窗口时才有效。