How to Use the "Handler" of Uialertaction to Call Another Uialertaction

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);

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)

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

Can a UIAlertAction trigger a function?

You can use a function as the handler, but it needs to have the correct type. Also you must not call it when you pass it as an argument, i.e., instead of handler: backToLogin() (which would set the return value of backToLogin as the handler) you would have handler: backToLogin without the ().

The following should work:

func backToLogin(alertAction: UIAlertAction) {
self.performSegueWithIdentifier("toLoginPage", sender: self)
}
let okButton = UIAlertAction(title: "OK", style: .Default, handler: backToLogin)

But having to change backToLogin might defeat the purpose, so you could just use a closure:

let okButton = UIAlertAction(title: "OK", style: .Default) { _ in
self.backToLogin()
}

How to call a function [do something] after a UIAlertAction?

Inside the alert controller add thie var

var callback:((Bool)->())?  

and when action happens inside handler of delete do

callback?(true/false)

and to listen

dAction.callback = { [weak self] (value) in 
print(value)
}


Related Topics



Leave a reply



Submit