You will need to add a property
@property (assign, nonatomic) NSTimeInterval previousTimeInterval;
Just copy the below lines of code and it should work.
- (void)updateTimer
{
// Create date from the elapsed time
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate];
timeInterval += self.previousTimeInterval;
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
self.dateFor = timerDate;
// Create a date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
// Format the elapsed time and set it to the label
NSString *timeString = [dateFormatter stringFromDate:timerDate];
self.stopwatchLabel.text = timeString;
}
- (IBAction)onStartPressed:(id)sender {
self.startDate = [NSDate date];
// Create the stop watch timer that fires every 10 ms
self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
//Disable start button and enable stop
self.startButton.enabled = NO;
self.stopButton.enabled = YES;
}
- (IBAction)onStopPressed:(id)sender
{
[self.stopWatchTimer invalidate];
self.stopWatchTimer = nil;
self.previousTimeInterval = [self.dateFor timeIntervalSince1970];
// enable start button and disable stop button
self.startButton.enabled = YES;
self.stopButton.enabled = NO;
}