我如何在Spritekit中呈现一个警报框?

问题描述 投票:0回答:1

我不知道如何在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)
sprite-kit alert
1个回答
0
投票

这就可以了,虽然不知道为什么会这样,但是确实可以了。

添加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)
    }

0
投票
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)
}
© www.soinside.com 2019 - 2024. All rights reserved.