Swift 3, Xcode 8.1. I want to display UIAlertController in UIViewController.
I have methods:
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
I call the method presentAlert in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
DefaultWireframe.presentAlert("test")
...
}
and got the warning:
Warning: Attempt to present UIAlertController: 0x7f904ac0d930 on UIViewController: 0x7f904ad08040 whose view is not in the window hierarchy!
How to avoid the warning and display the Alert?
It works when I try to show Alert in initial ViewController, but it doesn't work in another ViewController connected using push segue with initial VC.
rootViewControlleron which you're trying to presentUIAlertControlleris visible and not covered by another modal view controller for example? - dive