0

When presenting a UIAlert in SKScene nothing shows up Here is the code

 var alertController = UIAlertController(title: "Nothing Selected",
                                            message: "You have selected a picture.",
                                            preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "HI!", style: UIAlertActionStyle.cancel, handler: nil))
    self.view?.window?.rootViewController?.present(alertController, animated: true, completion: nil)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
B.Mahadewa
  • 35
  • 1
  • 7
  • Try this: https://stackoverflow.com/questions/39557344/swift-spritekit-how-to-present-alert-view-in-gamescene – Faysal Ahmed Mar 23 '18 at 09:28
  • Ask its view controller to do the dirty job. – El Tomato Mar 23 '18 at 09:36
  • Possible duplicate of [Swift & SpriteKit - How to present alert view in GameScene](https://stackoverflow.com/questions/39557344/swift-spritekit-how-to-present-alert-view-in-gamescene) – El Tomato Mar 23 '18 at 09:37

2 Answers2

2

From within the scene you need to present the Alert Controller at the root View Controller level.

if let vc = self.scene?.view?.window?.rootViewController {
    vc.present(alertController, animated: true, completion: nil)
}
Chrispy
  • 341
  • 3
  • 5
0

Try to present from the top view controller returned by this extension (taken from this post):

extension UIApplication {

    class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        return controller
    }

}
sundance
  • 2,930
  • 1
  • 20
  • 25