Метод UIAlertViewDelegate clickedButtonAtIndex не вызывается при реализации моего собственного делегата

Я хотел бы получить предупреждение от разных ViewControllers. Поскольку я хотел обрабатывать, нажимал ли пользователь последовательно "ОК" или "Отмена", я решил реализовать свой собственный UIAlertViewDelegate и вызывать его из моего UIViewController.

Проблема в том, что willDismissWithButtonIndex, didDismissWithButtonIndex и clickedButtonAtIndex никогда не вызываются, когда я нажимаю "OK" или "Отмена". Я знаю, что мой делегат привыкает, потому что 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()
    }

}

Что я делаю неправильно?

1 ответ

Решение

Эта переменная 'var делегат = AlertViewDelegate()' освобождена в конце области видимости, поэтому ничего не вызывается, попробуйте переместить delegate к собственности

Другие вопросы по тегам