NSTimer doesn't stop

cocoa-touch, iphone, timer

Solution

I should have been keeping a link to the timer by retaining it. :) I've asked this question 5 months ago, and it's amazing, how much more experience I acquired. :)

timer = [ [NSTimer scheduledTimerWithTimeInterval: ...] retain];
...
...
[timer invalidate];
[timer release];

Problem

I've got a timer which fires when the `viewWillAppear` method is being called and invalidates when the `viewDidDisappear` method is being called. But after a certain amount of switching between views the timer continues firing even after it was invalidated. What's the problem? Here is my code: ``` NSTimer *timer; - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; timer = [NSTimer scheduledTimerWithTimeInterval: 0.2f target: self selector:@selector( timerAction ) userInfo:nil repeats:YES]; } -(void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [timer invalidate]; timer = nil; } -(void) timerAction { NSLog(@"timerAction"); } ```

Original source

Related problems