Is it possible to wait for user touch inside a while loop? I mean, if I have a view with a “Finish” button inside and I have a loop like this:
while (ending == FALSE) {
...
/* Do something */
...
/* WAIT for user to touch "Finish" button and when this occurs, set ending=TRUE so the loop can finish */
...
/* Do something more*/
...
}
I thought of doing it with another while loop like this:
while (myVar == FALSE) {
/* Do nothing */
}
And modify the button action to change both variables (“ending” and “myVar”) but I think it’s not the most efficient solution and it would consume a lot of memory or CPU?
I’m quite sure your while loop will actually stall your program and not allow any touches to be obtained. If you really need to do something (update the screen for example) while waiting for a button to be pressed, you can use NSTimer to periodically activate a method. When the button is pressed it will trigger the associated answer at some point where your update method is not working on anything else. If what you need to do while waiting is not UI related, you may consider doing it on another thread instead of a periodic nstimer update.