34

I develop Stop Watch Application. In my application, there are Two UIButtons , StartBtn and StopBtn, And also I use NSTimer.

Now, i want to start NSTimer when user click on StartBtn and also stop when your click on StopBtn.

I know that NSTimer is stopped by [MyTimerName invalidate]; method but I don't know how to start NSTimer again?

  • possible duplicate of [iphone NStimer start in 2 seconds](http://stackoverflow.com/questions/2784809/iphone-nstimer-start-in-2-seconds) – Suresh Varma Aug 21 '12 at 10:41

3 Answers3

101

The NSTimer class is a bit awkward to use; rather than separating the creation/destruction from the start/stop, it's all rolled together. In other words the timer starts as soon as it's created and stops as soon as it's destroyed.

You therefore need to use the existence of the NSTimer object as a flag to indicate if it's running; something like this:

// Private Methods
@interface MyClass ()
{
    NSTimer *_timer;
}
- (void)_timerFired:(NSTimer *)timer;
@end

@implementation MyClass

- (IBAction)startTimer:(id)sender {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(_timerFired:)
                                                userInfo:nil
                                                 repeats:YES];
    }
}

- (IBAction)stopTimer:(id)sender {
    if ([_timer isValid]) {
        [_timer invalidate];
    }
    _timer = nil;
}

- (void)_timerFired:(NSTimer *)timer {
    NSLog(@"ping");
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • @AmitJarsaniya Yes you can; it creates a new `NSTimer` object when you start it and destroys it when you stop it. – trojanfoe Aug 21 '12 at 11:36
  • 1
    One correction: selector:@selector(_timerFired) should be selector:@selector(_timerFired:). Note the final colon after the method name (as its part of the method name in ObjC). – Cody A. Ray May 22 '13 at 20:08
  • @CodyA.Ray Nope; note the method is called `- (void)_timerFired` and not `- (void)_timerFired:(id)sender`. – trojanfoe May 23 '13 at 07:33
  • Oh, right. I think I also had to make _timerFired accept (NSTimer *)timer for this to work. Otherwise I got an unrecognized selector issue. – Cody A. Ray May 23 '13 at 16:30
  • @CodyA.Ray Yes, that is true, as documented in the `NSTimer` class reference. – trojanfoe May 26 '13 at 13:54
  • 1
    @trajanfoe: I think you should release the timer in `stopTimer:` because you retain it in `startTimer:`. The doc says `The NSRunLoop object removes and releases the timer`, but NSRunLoop must have retained it too. – Martin Jul 04 '13 at 16:04
  • @Martin Well spotted; that `retain` is an error I believe, so I've removed it. – trojanfoe Jul 04 '13 at 18:24
14

You could always use fire to start a NStimer again

  [timer fire];

To stop it:

  [timer invalidate];
  //remember to set timer to nil after calling invalidate;
  timer = nil;
Freddy
  • 810
  • 1
  • 9
  • 19
9

You can start the timer through

#define kRefreshTimeInSeconds 1
 NSTimer *myTimerName;
 .
 .
 myTimerName = [NSTimer scheduledTimerWithTimeInterval: kRefreshTimeInSeconds 
                                   target:self 
                                   selector:@selector(handleTimer:) 
                                   userInfo:nil
                                   repeats:YES];

Then the delegate function:

-(void)handleTimer: (id) sender 
{       
   //Update Values in Label here
}

And to stop Timer

-(void)stopTimer: (id) sender 
{       
   if(myTimerName)
   {
       [myTimerName invalidate];
        myTimerName = nil;
   }
}
Suresh Varma
  • 9,750
  • 1
  • 60
  • 91