【发布时间】:2017-04-01 15:27:47
【问题描述】:
Swift 3,Xcode 8.1。我想在UIViewController 中显示UIAlertController。
我有方法:
private static func rootViewController() -> UIViewController {
// cheating, I know
return UIApplication.shared.keyWindow!.rootViewController!
}
static func presentAlert(_ message: String) {
let alertView = UIAlertController(title: "RxExample", message: message, preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "OK", style: .cancel) { _ in })
rootViewController().present(alertView, animated: true, completion: nil)
}
Full code of this class you can find here
我在viewDidLoad中调用了presentAlert方法:
override func viewDidLoad() {
super.viewDidLoad()
DefaultWireframe.presentAlert("test")
...
}
并收到警告:
警告:尝试在 UIViewController: 0x7f904ad08040 上呈现 UIAlertController: 0x7f904ac0d930,其视图不在窗口层次结构中!
如何避免警告并显示警报?
当我尝试在初始 ViewController 中显示 Alert 时它可以工作,但它在使用 push segue 与初始 VC 连接的另一个 ViewController 中不起作用。
【问题讨论】:
-
您确定您尝试在其上显示
UIAlertController的rootViewController是可见的,并且不会被另一个模态视图控制器覆盖吗? -
在这个 viewController 上按下调试视图按钮(它在调试控制台上方的栏中)。在左侧,您将看到整个视图堆栈,窗口位于顶部。您可以右键单击任何视图以打印说明。将此与 rootViewController() 的视图进行比较;它必须显示您的 rootViewController 的视图如何不再在层次结构中。选择一个视图并使用它。
-
找到任何解决方案了吗?遇到同样的问题。
标签: ios swift uiviewcontroller