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") ... }
并得到警告:
Warning: Attempt to present UIAlertController: 0x7f904ac0d930 on UIViewController: 0x7f904ad08040 whose view is not in the window hierarchy!
如何避免警告并显示警报?
它在我尝试在初始ViewController中显示Alert时有效,但它在使用push segue与初始VC连接的另一个ViewController中不起作用.