Search code examples
iosobjective-cnstimer

Validate a NSTimer that was invalidated


How can I recall or reestablish a timer that was previously invalidated?

I am trying to allow the user to revalidate/restart a timer when they click a button. Is this possible?


Solution

  • When you invalidate a timer, set it to nil. Then you'll be able to point it to a new NSTimer object.

    Example:

    //When you're done using your timer
    [timer invalidate];
    timer = nil;
    
    //When you want to use it again
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:TRUE];