在UIWindow上显示UIAlertController

时间:2015-12-10 00:48:21

标签: ios swift uiwindow

我的UIWindow嵌套在我的所有导航控制器之上(它是一个可滑动,可拖动的迷你视频播放器)。我正在这样初始化它:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    let player = CustomWindowClass(frame:UIScreen.mainScreen().bounds)
    var window: UIWindow? {
        set {

        }
        get {

            return player
        }
    }
}

现在player的页面上有一个按钮,提示UIAlertController。我的问题是我不能presentViewController:直接来自UIWindow。我可以用:

let rootView = UIApplication.sharedApplication().keyWindow?.rootViewController
rootView?.presentViewController(theAlertController, animated: true, completion: nil)

..但它出现在UIWindow后面。我一整天都在搜索如何安全地显示我UIAlertController上方的UIWindow,但却空手而归。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

What's the size of the returned array of windows from UIApplication.sharedApplication().windows?

I'm guessing that it will be 2, and the second one in the array will be your new window. Try investigating each window in the returned array, and try presenting the UIAlertViewController on each of those to see if it has the desired effect.

eg, if there are two windows returned:

let controller = application.windows[1].rootViewController as UIViewController
controller.presentViewController(theAlertController, animated: true, completion: nil)

Reference: How to present UIAlertView from appDelegate