Sprite kit 如何在Spritekit中显示alertbox?

Sprite kit 如何在Spritekit中显示alertbox?,sprite-kit,alert,Sprite Kit,Alert,我不知道如何在Spritekit中获得警报框。下面的代码过去可以使用,但KeyWindow已被弃用,所以我现在该怎么办 let currentViewController : UIViewController=UIApplication.shared.keyWindow!.rootViewController! 我试过: let currentViewController : UIViewController = (self.view?.window!.rootViewController)!

我不知道如何在Spritekit中获得警报框。下面的代码过去可以使用,但KeyWindow已被弃用,所以我现在该怎么办

let currentViewController : UIViewController=UIApplication.shared.keyWindow!.rootViewController!
我试过:

let currentViewController : UIViewController = (self.view?.window!.rootViewController)!
任何其他一些变体,但似乎都不起作用。警报不会弹出

有人能解释一下这个问题吗

完整代码块:

 let ac = UIAlertController(title: "Connect to others", message: nil, preferredStyle: .actionSheet)
 ac.addAction(UIAlertAction(title: "Host a session", style: .default, handler: startHosting))
 ac.addAction(UIAlertAction(title: "Join a session", style: .default, handler: joinSession))
 ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

 let currentViewController :
    UIViewController = (self.view?.window!.rootViewController)!

 currentViewController.present(ac, animated: true, completion: nil)

这是有效的,不确定为什么会这样,但它确实有效

添加DispatchQueue

DispatchQueue.main.async {
    let ac = UIAlertController(title: "Connect to others", message: nil, preferredStyle: .actionSheet)
        ac.addAction(UIAlertAction(title: "Host a session", style: .default, handler: self.startHosting))
        ac.addAction(UIAlertAction(title: "Join a session", style: .default, handler: self.joinSession))
    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        self.view?.window?.rootViewController!.present(ac, animated: true, completion: nil)
    }

此解决方案不会打开警报框。。我不得不添加“DispatchQueue.main.async”
let alert = UIAlertController(title: "Restore Purchases?", message: "Do you want to restore a purchase?", preferredStyle: .alert)
            let ok = UIAlertAction(title: "OK", style: .default, handler: { action in

                //run code here when ok button is pressed

                 })

            alert.addAction(ok)
            self.view?.window?.rootViewController?.present(alert, animated: true, completion: nil)
}