【问题标题】:How present a UIAlert in a Skscene如何在 Skscene 中呈现 UIAlert
【发布时间】:2019-04-15 07:59:29
【问题描述】:
在 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)
【问题讨论】:
标签:
ios
swift
uialertcontroller
skscene
【解决方案1】:
在场景中,您需要在根视图控制器级别呈现警报控制器。
if let vc = self.scene?.view?.window?.rootViewController {
vc.present(alertController, animated: true, completion: nil)
}
【解决方案2】:
尝试从此扩展返回的顶视图控制器呈现(取自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
}
}