I'd like to popup an alert from different ViewControllers. Because I wanted to handle whether the user clicked "OK" or "Cancel" consistently, I decided to implement my own UIAlertViewDelegate and call it from my UIViewController.
The problem is that willDismissWithButtonIndex, didDismissWithButtonIndex, and clickedButtonAtIndex are never called when I click "OK" or "Cancel". I know that my delegate is getting used, because willPresentAlertView() and didPresentAlertView() are getting called when the alert displays.
Here is the code for my delegate:
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")
}
}
And here is the code for my 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()
}
}
What am I doing wrong?
this variable 'var delegate = AlertViewDelegate()' deallocated at end of scope
so nothing called, try move delegate
to property