I'm tired of declaring entire classes as having the ability to handle UIAlertView
clicks by making them extend UIAlertViewDelegate
. It starts to feel messy and wrong when I have multiple possible UIAlertView
s, and have to distinguish which was clicked in the handler.
What I really want is to create a single object that implements the UIAlertViewDelegate
protocol, and give this one-off object to my UIAlertView
when showing it.
I want something like this:
let confirmDelegate = UIAlertViewDelegate() {
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
// Handle the click for my alertView
}
}
And then use it when showing the alert:
let alertView = UIAlertView(title: "Confirm", message: "Are you sure?", delegate: confirmDelegate, cancelButtonTitle: "No", otherButtonTitles: "Yes")
alertView.show()
Is this possible without declaring a new class?
I understand I could do something like this:
class ConfirmDelegate: UIAlertViewDelegate {
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
// ...
}
}
And then instantiate a ConfirmDelegate()
, but I'm just wondering if this is possible as one-liner class declaration and instantiation.
UIAlertController
class that is block based. – HamilUIAlertController
, thank you – SciurineUIAlertView
that implements this behavior. (They don't use anonymous inner classes, though.) – Vanward