這裏有一個解決方案,我在斯威夫特3.使用這是顯示警告給用戶的功能,如果你把它多次用戶已經駁回了警報之前,它會將新的警報文本添加到已顯示的警報中。如果顯示其他視圖,則不會顯示警報。並非所有人都會同意這種行爲,但對於簡單的情況,這種方式很有效。
extension UIViewController {
func showAlert(_ msg: String, title: String = "") {
if let currentAlert = self.presentedViewController as? UIAlertController {
currentAlert.message = (currentAlert.message ?? "") + "\n\nUpdate:\(title): \(msg)"
return
}
// create the alert
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
}
[此線索] [1]講解如何正確解決類似問題 [1]:http://stackoverflow.com/questions/21179922/can-i-check-if- any-uialertview-displays-right-now – 2014-11-18 06:05:23
其他線程中的解決方案非常脆弱和難看,並且可能會在iOS8中破解。 – fabb 2014-11-23 09:31:52