How to stop/invalidate NStimer

Shishir.bobby picture Shishir.bobby · Mar 2, 2013 · Viewed 19.7k times · Source

I am using

 [NSTimer scheduledTimerWithTimeInterval:0.1f
                                  target:self
                                selector:@selector(update)
                                userInfo:nil
                                 repeats:YES];

I want to stop calling this timer one.

viewDidDisappear

How can i do that? invalidate?

Answer

βhargavḯ picture βhargavḯ · Mar 2, 2013

Declare NSTimer *myTimer in .h file.

Assign instance as tom said like this

myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f
                                     target:self
                                   selector:@selector(update)
                                   userInfo:nil
                                    repeats:YES];

Stop and Invalidate using this

- (void) viewDidDisappear:(BOOL)animated
{
    [myTimer invalidate];
    myTimer = nil;
}