我正在定义一个符合 UIAlertViewDelegate 的类:
class PaymentAlert: NSObject, UIAlertViewDelegate {
var orderId: Int!
var items: [String]
init(orderId: Int, items: [String]) {
self.orderId = orderId
self.items = items
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
println(index)
}
}
然后我在另一个类(class)做了这些 Action :
var alertClass = PaymentAlert(orderId: orderId, items: items)
var alert = UIAlertView(title: "Payment declined", message: "Do you want to retry?", delegate: alertClass, cancelButtonTitle: "Cancel", otherButtonTitles: "Retry")
alert.show()
一切运行良好,除了当我单击任何按钮时 clickedButtonIndex 委托(delegate)函数永远不会被调用,我尝试将委托(delegate)设置为 alertClass.self,但这并没有做太多。
为什么永远不会调用 clickedButtonIndex 函数?
最佳答案
UIAlertView
的 delegate
属性是 weak
因此您的 PaymentAlert
实例在您点击按钮之前被销毁.
您必须在某处保持对 PaymentAlert
实例的强引用,直到警报被解除。
关于ios - 单独类中的 UIAlertViewDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26244380/