A simplified set of methods to demonstrate what’s happening:
- (void)timerDidFire {
NSLog(@"fire");
}
- (void)resetTimer:(NSTimer *)timer {
if (timer) [timer invalidate]; // timer = nil; here doesn't change anything
NSLog(@"%@", timer);
timer = [NSTimer ...Interval:1 ... repeats:YES];
}
- (IBAction)pressButton {
[self resetTimer:myTimer];
}
Clearing I’m doing something wrong, but what? Why do I get an extra timer with every press?
Each time the
resetTimer:method is called you create a newNSTimerinstance. Unfortunately after the execution of this method is finished you lost all your references to the new instance because it was assigned to a local variable.The timer you create inside the method is not assigned to the myTimer variable. Whatever myTimer is, it is not the new created timer.
You could dump all those local variables and simply use something like this: