How to present UIAlertController from SKScene

后端 未结 3 1917
鱼传尺愫
鱼传尺愫 2021-01-22 23:01

I\'m working in Spritekit and I\'m trying to present a UIAlertController from my SKScene, but I am having trouble doing it. I\'ve watched several tutorials but none of the UIAle

3条回答
  •  长情又很酷
    2021-01-22 23:34

    The SKScene instance can't invoke presentViewController(_:animated:completion) because it is not a subclass of UIViewController. However, if you rewrite as such, your alert will launch:

    self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
    

    ps: there will be a warning though that Attempt to present on which is already presenting. If anyone knows how to eradicate this warning, that will be great.


    [Updated 11 August 2016]

    To eradicate the aforementioned warning, check if the rootViewController has presented a view controller:

     let vc = self.view?.window?.rootViewController
     if vc.presentedViewController == nil {
        vc.presentViewController(alert, animated: true, completion: nil)
     }
    

提交回复
热议问题