UIAlertViewDelegate 方法 clickedButtonAtIndex 在实现我自己的委托时没有被调用

UIAlertViewDelegate method clickedButtonAtIndex not getting called when implementing my own delegate

我想弹出来自不同 ViewController 的警报。因为我想处理用户是否一致地单击 "OK" 或 "Cancel",所以我决定实现我自己的 UIAlertViewDelegate 并从我的 UIViewController.

调用它

问题是当我单击 "OK" 或 "Cancel" 时,willDismissWithButtonIndex、didDismissWithButtonIndex 和 clickedButtonAtIndex 从未被调用。我知道我的委托正在被使用,因为 willPresentAlertView() 和 didPresentAlertView() 在警报显示时被调用。

这是我的代表的代码:

import UIKit

class AlertViewDelegate: NSObject, UIAlertViewDelegate {
    func willPresentAlertView(alertView: UIAlertView) {
        println("will present")
    }

    func didPresentAlertView(alertView: UIAlertView) {
        println("did present")
    }

    func alertViewCancel(alertView: UIAlertView) {
        println("cancelled")
    }

    func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
        println("will dismiss")
    }

    func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
        println("did dismiss")
    }

    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        println("clicked")
    }
}

这是我的 ViewController:

的代码
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var delegate = AlertViewDelegate()
        var alert = UIAlertView(title: "alert", message: "blah blah", delegate: delegate, cancelButtonTitle: "cancel", otherButtonTitles: "OK")
        alert.show()
    }

}

我做错了什么?

此变量 'var delegate = AlertViewDelegate()' 在作用域末尾释放 所以什么都没有,尝试将 delegate 移动到 属性