Как представить окно оповещения в Spritekit? - PullRequest
0 голосов
/ 02 мая 2020

Я не могу понять, как получить окно оповещения в 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)

Ответы [ 2 ]

0 голосов
/ 07 мая 2020
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)
}
0 голосов
/ 03 мая 2020

это работает, не совсем уверен, почему это работает так, но это работает.

добавление 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)
    }
...