如何在Skscene中呈现UIAlert

时间:2018-03-23 09:19:26

标签: ios swift uialertcontroller skscene

在SKScene中展示UIAlert时,没有任何显示 这是代码

 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)

2 个答案:

答案 0 :(得分:2)

您需要在场景中以根View Controller级别显示Alert Controller。

if let vc = self.scene?.view?.window?.rootViewController {
    vc.present(alertController, animated: true, completion: nil)
}

答案 1 :(得分:0)

尝试从此扩展程序返回的顶视图控制器(取自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
    }

}