在 UIWindow 上显示 UIAlertController

Display UIAlertController over UIWindow

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

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

        }
        get {

            return player
        }
    }
}

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

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

..但它出现在我的 UIWindow 后面。我整天都在寻找如何安全地在我的 UIWindow 上方显示 UIAlertController,但一无所获。有什么建议吗?

UIApplication.sharedApplication().windows 返回的 windows 数组的大小是多少?

我猜它会是 2,数组中的第二个将是您的新 window。尝试调查返回数组中的每个 window,并尝试在每个 UIAlertViewController 上显示它以查看它是否具有预期的效果。

例如,如果有两个 windows 返回:

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

参考: