Swift2 加载UIAlertController时出错
我想在internet连接不可用时加载警报。检查internet连接的功能已就绪,但我无法加载警报。因此,我只是在没有任何条件的情况下将警报代码放入viewDidLoad中,并得到以下错误: 警告:尝试在x上显示UIAlertController:0x12752d400。其视图不在窗口层次结构中的ViewController:0x127646f00 代码:Swift2 加载UIAlertController时出错,swift2,ios9,uialertcontroller,Swift2,Ios9,Uialertcontroller,我想在internet连接不可用时加载警报。检查internet连接的功能已就绪,但我无法加载警报。因此,我只是在没有任何条件的情况下将警报代码放入viewDidLoad中,并得到以下错误: 警告:尝试在x上显示UIAlertController:0x12752d400。其视图不在窗口层次结构中的ViewController:0x127646f00 代码: 你能告诉我怎么修吗?这个错误告诉你出了什么问题 您试图在viewdiload中显示视图控制器,但视图虽然已加载,但不在任何层次结构中。尝试将
你能告诉我怎么修吗?这个错误告诉你出了什么问题
您试图在
viewdiload
中显示视图控制器,但视图虽然已加载,但不在任何层次结构中。尝试将代码放入viewdideappeage
方法中,该方法在视图出现在屏幕上后调用,并且位于视图层次结构中 Swift 4更新
我认为这可以帮助您解决您的问题:
override func viewDidLoad() {
super.viewDidLoad()
verificationCode.delegate = self
let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .alert)
let delete = UIAlertAction(title: "OK", style: .default) { (_) in }
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alert.addAction(cancelAction)
alert.addAction(delete)
alert.popoverPresentationController?.sourceView = sender as UIView
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
if (!Util.isConnectedToNetwork()) {
self.isConnected = false
}
}
将代码从viewDidLoad移动到ViewDidDisplay
override func viewDidAppear(animated: Bool) {
// Delegates
verificationCode.delegate = self
let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in }
alert.addAction(action)
self.presentViewController(alert, animated: true) {}
if (!Util.isConnectedToNetwork()) {
self.isConnected = false
}
}
override func viewDidAppear(animated: Bool) {
// Delegates
verificationCode.delegate = self
let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in }
alert.addAction(action)
self.presentViewController(alert, animated: true) {}
if (!Util.isConnectedToNetwork()) {
self.isConnected = false
}
}