Acoop
Acoop

Reputation: 2666

Pause function until user inputs

I have a function that returns a Bool value depending on user input through a UIAlertController. Due to the setup of the function, I must return the Bool outside of the user response code. I have a variable that will carry the Bool value until it is returned, but since the whole function evaluates before the user responds, the response has no effect. Is there a way to prevent the function from proceeding until given a response?

Upvotes: 0

Views: 1092

Answers (1)

Ahmad Amin
Ahmad Amin

Reputation: 201

I'm assuming your code looks similar to

let alert = UIAlertController(title: "test", message: "test", preferredStyle: UIAlertControllerStyle.Alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
    println("EXECUTE CODE HERE")
}
alert.addAction(ok)

self.presentViewController(alert, animated: true, completion: nil)

Instead of trying to pause the execution of the code (which would freeze the entire app, if you could) you should call the function in the callback block EXECUTE CODE HERE.

If some of the code in your function needs to be executed before the alert then just separate your code into two functions or just place the code that should be executed after the return inside the callback block

Upvotes: 2

Related Questions