Here’s a code snippet I’m trying to get to work, but its loop won’t stop the way that I want it to:
- (IBAction)methodName:(UIButton*)sender
{
[self loopMethod];
}
-(void) loopMethod
{
for( int index = 0; index < loopLimit; index++ )
{
//code I want to execute
[self performSelector:@selector(loopMethod)
withObject:nil
afterDelay:2.0];
}
}
The code just keeps looping even though I’ve made the for loop finite. What I want is for the code to execute, pause for two seconds, and then run the loop while the int value is less than the loopLimit I’ve set.
It’s been hinted that this performSelector:withObject:afterDelay: method may not be the right thing to use here but I’m not sure why or what is better to use here.
Any illuminating suggestions?
What’s happening here is that the loop is running as quickly as possible, and the
performSelector:...calls are happening at that speed. Then, at 2.0001, 2.0004, 2.0010, … seconds later,methodgets called.The other thing (now that you’ve edited to make clear that the
performSelector:...is ending up calling the same method that it’s in, is that the value of your loop’sindexvariable isn’t saved between calls. Every timeloopMethodis run, the code in the loop starts from the beginning:indexis set to zero and counts up. That means that every time the method is run, you end up withloopLimitnew calls pending, 2 seconds from then. Each one of those calls in turn spawns a new set, and so on, ad infinitum.Every run of the loop is in fact finite, but the loop keeps getting run. You need some way to signal that the loop needs to stop, and you can’t do that entirely within the loop method. You could put the counter (your
indexvariable) into an ivar; that would make its value persistent across calls toloopMethod, but I think you want to look into using anNSTimerthat repeats:If you stick this into an ivar, you can keep track of how many times it fires and stop it later. There’s a number of posts already on SO about updating text fields in a loop, using a timer like this: https://stackoverflow.com/search?q=%5Bobjc%5D+update+text+field+timer