IOS: stop a NSTimer [duplicate] IOS: stop a NSTimer [duplicate] ios ios

IOS: stop a NSTimer [duplicate]


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];


One way to do it is to create NSTimer *timer; as a global variable so you can have a handle to the timer. Some thing likes this:

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

To stop timer somewhere in the same class:

[timer invalidate];