How to stop NSTimer

后端 未结 7 593
我寻月下人不归
我寻月下人不归 2020-12-03 06:33

Hey so i am just making an example application and i am having a little trouble, So i have a table view and then i have a few rows and when a user clicks a row it takes them

相关标签:
7条回答
  • 2020-12-03 07:33

    4 year old post, I know but maybe I can save the NEXT guy from wasting time trying invalidate a NSTimer. Apples documentation explains it and it works for me.

    in .h

    @property(nonatomic, retain) NSTimer *yourTimer;
    @property(weak) NSTimer *repeatingTimer;
    

    in .m

    @synthesize yourTimer, repeatingTimer;
    

    in viewDidLoad

    yourTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimerInterval)(40.0/60.0)
                                                 target:self
                                               selector:@selector(blink)
                                               userInfo:nil
                                                repeats:TRUE];
    
      self.repeatingTimer = yourTimer;
    

    in my viewDidDisappear

       [repeatingTimer invalidate];
       repeatingTimer = nil;
    

    the trick is in the assignment of the first NSTimer to (weak) NStimer object and killing THAT object.

    0 讨论(0)
提交回复
热议问题