简体   繁体   English

如何在Skscene中呈现UIAlert

[英]How present a UIAlert in a Skscene

When presenting a UIAlert in SKScene nothing shows up Here is the code 在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)

From within the scene you need to present the Alert Controller at the root View Controller level. 在场景中,您需要在根View Controller级别上显示Alert Controller。

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

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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM