Writing Handler for Uialertaction

Writing handler for UIAlertAction

Instead of self in your handler, put (alert: UIAlertAction!). This should make your code look like this

    alert.addAction(UIAlertAction(title: "Okay",
style: UIAlertActionStyle.Default,
handler: {(alert: UIAlertAction!) in println("Foo")}))

this is the proper way to define handlers in Swift.

As Brian pointed out below, there are also easier ways to define these handlers. Using his methods is discussed in the book, look at the section titled Closures

How to use the handler of UIAlertAction to call another UIAlertAction?

Yes of course! It's possible. Try something like that:

let alertController = UIAlertController.init(title: "Title", message: "Message", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Title", style: .default, handler: { (action) in
self.someFunction()
}))
self.present(alertController, animated: true, completion: nil)

Here's your function:

func someFunction() {
let alertController = UIAlertController.init(title: "Some Title", message: "Some Message", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Title For Button", style: .default, handler: { (action) in
// Completion block
}))
self.present(alertController, animated: true, completion: nil)
}

Here's your problem line:

let action = UIAlertAction(title: "Who's there??", style: .default, handler: joke())

You can easily change it to:

let action = UIAlertAction(title: "Who's there??", style: .default, handler: { (action) in
// Completion block
})

Hope it helps!

How to pass multiple handlers in UIAlertAction

It's not totally clear what you're asking, but if you are trying to figure out which button was pressed so that you can execute different methods for each one you can do something like this:

@IBAction func buttonClicked(_ sender: UIButton) {
let alert = UIAlertController(title: "Select Value", message: nil, preferredStyle: .actionSheet)
for list in self.listValue {
alert.addAction(UIAlertAction(title: list.value, style: .default, handler: { (action) in
// How do I call different handlers here?
// I'll need to retrieve alert.title in these handlers
switch action.title {
case "Value A":
print("It's Value A")
case "Value B":
print("It's Value B")
default:
print("We didn't implement anything for this value")
}
}))
}
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: false, completion: nil)
}

How can I perform the handler of a UIAlertAction?

After some experimentation I just figured this out. Turns out that the handler block can be cast as a function pointer, and the function pointer can be executed.

Like so

//Get the UIAlertAction
UIAlertAction *action = self.handlers[buttonIndex];

//Cast the handler block into a form that we can execute
void (^someBlock)(id obj) = [action valueForKey:@"handler"];

//Execute the block
someBlock(action);

UIAlertAction handler is non escaping?

UIAlertAction handler is escaping

because Optional closures are all implicitly escaping (thanks to @sweeper)

Add a call to a function from within a UIAlertController

You just need to change how you declare your searchTheWeb action to the following:

let search = UIAlertAction(title: "Search The Web!", style: .default) { [weak self] _ in
self?.searchTheWeb()
}
alert.addAction(search)

Right way to make a UIAlertAction's handler

They are all the same, it's mostly a matter of syntactic style you prefer. Option 3 uses type inference and the trailing closure syntax, which is generally preferred as it's concise and gets rid of the extra set of parentheses as it moves the last argument closure outside of the function call. You could one-up option 3 by removing the parentheses around action, those aren't needed either.

More on this is explained in the Swift Programming Language book, see the section on Closures.



Related Topics



Leave a reply



Submit