I just would like to get a second opinion on if there's any unforeseen issue with calling IBAction functions recursively. I am asking this question because of a crash that happened once and I found it hard to replicate.
Let say we have this call:
var allowedToSend = false
@IBAction func emailButtonPressed(sender: UIButton) {
if !allowedToSend {
let controller = UIAlertController(title: title,
message: "Are you sure you want to send the Email?",
preferredStyle: .Alert)
controller.addAction(UIAlertAction(title: "Yes please, send.", style: .Default, handler: {
action in
self.allowedToSend = true;
self.emailButtonPressed(sender) // call self again
}))
controller.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil))
presentViewController(controller, animated: true, completion: nil)
return // exit the function
}
// Reset the value
allowedToSend = false
// Sending code
let text = textArea.text
let to = toLabel.text
....
}
Is this the right way to call an @IBAction
recursively in Swift?
Yes, this is the correct way to call the function recursively like you would with any other function.
Note:
It is not a good habit to create this many UIControllers, regardless of their type.
Also, since your recursion is not designed to end, this will lead to an infinite number of calls and probably cause the program to crash from a stack overflow - I would not recommend that.