objective c - Pausing iteration of a for loop to wait for user input -
i wrote for
loop iterating through array of objects. asking myself if it's possible break iteration of loop until user clicks on button calls ibaction
?
for (int = 0; < [array count]; i++) { // object // wait action method called // user clicked action go on }
you can adapt code fit case. "unrolls" loop multiple messages. start sequence [self doitforindex:[nsnumber numberwithint:0]];
- (bool)candoitforindex:(nsnumber *)i { // return yes if want go ahead // (e.g. test bool set in response user tapping button } - (void)waitforindex:(nsnumber *)i { if ([self candoitforindex:i]) { // clean // repeat i+1: [self doitforindex:[nsnumber numberwithint:[i intvalue]+1]]; } else { [self performselector:_cmd withobject:i afterdelay:0.01f; } } - (void)doitforindex:(nsnumber *)i { if ([i intvalue] < lastindex) { // have [self waitforindex:i]; } // else you're done }
apple's nsrunloop
concept expects complete processing pretty quickly. if tie main thread waiting something, nothing else in app can happen. above code breaks "wait" multiple message sends, , keeps app responsive.
Comments
Post a Comment