我有一个UIAlertController,如果没有互联网连接,它会弹出。当连接恢复时,我不知道如何恰当地忽略它。因为我每10秒就会检查一次,如果我这样做了:
self.dismissViewControllerAnimated(true, completion: nil)一旦警报视图已经关闭,这将在每隔一段时间关闭主视图。我试过了:
let alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert)
if Reachability.isConnectedToNetwork() == true {
print("Internet connection OK")
alert.dismissViewControllerAnimated(true, completion: nil)
} else {
print("Internet connection FAILED")
self.presentViewController(alert, animated: true, completion: nil)
}但是alert.dismissViewControllerAnimated(true, completion: nil)不能工作。谢谢!
发布于 2016-03-27 14:32:13
我认为,问题是多次呈现UIAlertController,并尝试取消UIAlertController的一个实例。因此,您需要先关闭UIAlertController,然后再显示它,如下所示
let alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: .alert)
if Reachability.isConnectedToNetwork() == true {
print("Internet connection OK")
alert.dismiss(animated: true, completion: nil)
} else {
print("Internet connection FAILED")
alert.dismiss(animated: true, completion: nil)
self.present(alert, animated: true, completion: nil)
}发布于 2016-03-28 02:33:22
这对我很有效:谢谢你,@OS_Binod和@Pyro
let alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert)
if Reachability.isConnectedToNetwork() == true {
print("Internet connection OK")
self.dismissViewControllerAnimated(false, completion: nil)
} else {
print("Internet connection FAILED")
alert.dismissViewControllerAnimated(false, completion: nil)
self.presentViewController(alert, animated: false, completion: nil)
}发布于 2016-03-27 14:32:50
我认为问题可能是您试图在显示警报之前关闭警报
更新:如果您只想在互联网恢复后才解除警报,请在显示后保留'UIAlertController‘的引用,一旦您可以使用该对象将其解除
在您的情况下,您正在尝试消除新创建的对象的“警告”,这些对象甚至没有显示,因此当internet重新出现时,您需要在创建新对象之前清除保留在参考中的旧对象
此外,如果您每10秒调用一次该方法,请在呈现新的警报之前清除旧的警报,以避免出现多个警报。否则,如果警报已经显示,则可以设置一个标记。方法不会每隔10秒再次检查一次,直到警报被清除为止。
更好的选择是:检查外部的互联网连接,如果没有互联网连接,则只创建UIAlertController
更新1您可以将警报作为全局变量,并在else块中创建警报对象时将其存储,当internet恢复时,您可以检查警报是否有对象,如果有,则可以取消警报
if Reachability.isConnectedToNetwork() == true {
print("Internet connection OK")
if alert != nil {
alert.dismissViewControllerAnimated(true, completion: nil)
}
//dismiss the alert using old refence from the alert
//continue your normal code
} else {
alert = UIAlertController(title: "No internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: UIAlertControllerStyle.Alert)
//you may check and dismiss the previous alertcontroller before presenting the new one
print("Internet connection FAILED")
self.presentViewController(alert, animated: true, completion: nil)
}https://stackoverflow.com/questions/36244445
复制相似问题