How to check if NSTimer has been already invalidated

前端 未结 4 2035
长情又很酷
长情又很酷 2021-02-19 19:07

I have a NSTimer object that I need to invalidate if a user taps on a button or if they exit a view.

So I have:

[myNSTimer invalidate];
         


        
4条回答
  •  误落风尘
    2021-02-19 19:42

    I have had the same problem while I was trying to pause & restart a timer. To stop it:

    [timer invalidate];
    [timer release];
    timer = nil;
    

    And to start/ restart it:

    timer = [[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(aSelector) userInfo:nil repeats:YES] retain];
    

    Now it doesn't crash when I invalidate it.

提交回复
热议问题