How do I get the keyWindow reference in a swift app?

后端 未结 8 991
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-12 03:55

In Objective-C in the viewDidLoad method of a UIViewController I do this to get the keyWindow reference in my iOS app:

 UIWindow * keyWindow = [[UIApplication sh         


        
8条回答
  •  -上瘾入骨i
    2021-02-12 04:21

    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:

    if let app = UIApplication.sharedApplication().delegate as? AppDelegate, let window = app.window {
        MBProgressHUD.show(text, view:window)
    }
    

    Updated for Swift 3: (Thanks @Trevor)

    if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
        MBProgressHUD.show(text, view:window)
    }
    

提交回复
热议问题