ios - 单独类中的 UIAlertViewDelegate

标签 ios swift

我正在定义一个符合 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 函数?

最佳答案

UIAlertViewdelegate 属性是 weak 因此您的 PaymentAlert 实例在您点击按钮之前被销毁.

您必须在某处保持对 PaymentAlert 实例的强引用,直到警报被解除。

关于ios - 单独类中的 UIAlertViewDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26244380/

相关文章:

javascript - UIWebView 不使用动态链接在 Button 上调用 shouldStartLoadWithRequest

ios - Firebase 错误,即使我不使用它

iOS Swift float 操作按钮在更改屏幕时移动

swift - 如何在另一个函数中使用一个函数的变量?

ios - 无法从生成的 QRCode 中获取信息

ios - 为什么在 iOS 中使用 View 模型 (MVVM)?

ios - WKWebView - 无法加载页面并使用 loadHTMLString 设置自定义字体/css

ios - 为 iOS 应用程序选择正确的后端语言

ios - Swift ESTabBarController 大中心按钮失败

ios - 意外行为文本 textFieldShouldBeginEditing/textFieldDidBeginEditing