IOS: stop a NSTimer

cyclingIsBetter picture cyclingIsBetter · Dec 15, 2011 · Viewed 38.6k times · Source

Possible Duplicate:
NSTimer doesn't stop

I have this code:

[NSTimer scheduledTimerWithTimeInterval:110.0
                                 target:self
                               selector:@selector(targetMethod:)
                               userInfo:nil
                                repeats:YES];

- (void) targetMethod:(NSTimer) timer{
NSLog(@"Hello World");}

in targetMethod I can stop timer with [timer invalidate], but out of this method, How can I stop targetMethod?

Answer

Raphael Petegrosso picture Raphael Petegrosso · Dec 15, 2011

You can keep your NSTimer in a variable and stop the timer using the invalidate method. Like the following:

NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:110.0
                                 target:self
                               selector:@selector(targetMethod:)
                               userInfo:nil
                                repeats:YES];

[myTimer invalidate];