如果我在一个控制器上有多个AlertView,如果它们都被分别触发,如何让它只显示最后一个AlertView?请看下面的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.getAPI1()
self.getAPI2()
self.getAPI3()
}
func getAPI1() {
do {
// try ... Get API Process
} catch {
let alertView = UIAlertView(title: "ERROR", message: "There is an error on getAPI1()", delegate: nil, cancelButtonTitle: "OK")
alertView.show()
}
}
func getAPI2() {
do {
// try ... Get API Process
} catch {
let alertView = UIAlertView(title: "ERROR", message: "There is an error on getAPI2()", delegate: nil, cancelButtonTitle: "OK")
alertView.show()
}
}
func getAPI3() {
do {
// try ... Get API Process
} catch {
let alertView = UIAlertView(title: "ERROR", message: "There is an error on getAPI3()", delegate: nil, cancelButtonTitle: "OK")
alertView.show()
}
}由于getAPI1、getAPI2和getAPI3无论哪一个出错都需要执行,如何只显示最后一个AlertView?谢谢。
发布于 2016-02-02 10:49:47
您可以使用一个变量来存储当前处于活动和显示状态的警报。
var activeAlert : UIAlertView! = nil每次显示警报时,请检查之前是否显示了任何警报。你可以像这样检查它
if nil != activeAlert {
activeAlert.dismissWithClickedButtonIndex(-1, animated: false)
//show your new alert here
}这样可以确保总是显示最后一个警报。确保您正在实现UIAlertViewDelegate协议的alertView:clickedButtonAtIndex:方法。此外,iOS 9.0已弃用UIAlertView,因此我建议您改用UIAlertViewController
https://stackoverflow.com/questions/35144042
复制相似问题