How to stop NSTimer started on button click in this timer callback method

ios, nstimer, objective-c

Solution

It's funny how quick you can answer your own question after you asked for a help. No matter how long you have been struggling to find the answer before by yourself:

[self performSelectorOnMainThread:@selector(stopTimer) withObject:nil waitUntilDone:YES];

And the selector:

- (void) stopTimer
{
    [timer invalidate];
    timer = nil;
}

Problem

I am having problems with stoping `NSTimer` started on button click. `[timer invalidate]` and `timer = nil;` just do nothing neither when I am trying to stop it `viewWillDisappear` nor in method invoked by method which is being invoked by this timer. However when I start my timer in `viewWillAppear` and invalidate it in `viewWillDisappear` everything is fine. I suppose there might be an issue in thread I am starting the timer from. Can you help with that? I looked through all answers here regarding `NSTimer` not stopping, but they didn't help to solve the problem. The way I initialize my timer: ``` timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(oneSecondPassedSinceRoundStarted) userInfo:nil repeats:YES]; ``` The ways I tried to stop it: ``` [self.timer invalidate]; [timer invalidate]; ```

Original source