iOS. Attempt to present UIAlertController on UIViewController whose view is not in the window hierarchy
<p>Swift 3, Xcode 8.1. I want to display <code>UIAlertController</code> in <code>UIViewController</code>.</p>

<p>I have methods:</p>

<pre><code>private static func rootViewController() -&gt; 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)
}
</code></pre>

<p><a href="https://github.com/ReactiveX/RxSwift/blob/a9b5da4e8b828fc3490df0b4c47d204952450b39/RxExample/RxExample/Services/Wireframe.swift" rel="noreferrer">Full code of this class you can find here</a></p>

<p>I call the method presentAlert in <code>viewDidLoad</code>:</p>

<pre><code>override func viewDidLoad() {
    super.viewDidLoad()
    DefaultWireframe.presentAlert("test")
    ...
}
</code></pre>

<p>and got the warning:</p>

<blockquote>
  <p>Warning: Attempt to present UIAlertController: 0x7f904ac0d930 on UIViewController: 0x7f904ad08040 whose view is not in the window hierarchy!</p>
</blockquote>

<p>How to avoid the warning and display the Alert?</p>

<p><strong>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.</strong></p>