Swift 3、Xcode 8.1。 UIViewController
にUIAlertController
と表示したいiOS。ビューがウィンドウ階層にない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:0x7f904ac0d930をUIAlertControllerに提示しようとする誘惑:0x7f904ad08040ビューはウィンドウ階層にありません!
アラートを表示して警告を表示するにはどうすればよいですか?
初期ViewControllerで警告を表示しようとすると動作しますが、初期VCでプッシュセグを使用して接続された別のViewControllerでは動作しません。
'UIAlertController'を表示しようとしている' rootViewController'が見えていて、別のモーダルビューコントローラで覆われていないことは確かですか? – dive
このviewControllerのデバッグビューボタン(デバッグコンソールのバーにある)を押します。左側にはウィンドウ全体が一番上に表示されます。任意のビューを右クリックして、説明を印刷できます。これをrootViewController()のビューと比較してください。あなたのrootViewControllerのビューがもはや階層内にどうなっていないかを示すケースが必要です。そのビューを選択し、その代わりに使用します。 –
すべてのソリューションが見つかりましたか?同じ問題に遭遇する。 –