在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)
答案 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
}
}