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中不起作用.
解决方法
在viewDidLoad中,您的应用尚未向用户显示视图控制器,因此无法显示警报.尝试在viewDidAppear中执行该代码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。