Question

I've looked at the other pause/resume timer questions but couldn't figure out how to fix my problem. I just have a simple timer label and two buttons, a start and a stop button. Let me know if you need any more info on my project to help answer my question. Can anyone see where I'm going wrong?

- (void)updateTimer
{
    // Create date from the elapsed time
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

    // 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;
}

- (void)updateResumeTimer
{
    // Format the elapsed time and set it to the label
    NSString *timeString = [dateFormat stringFromDate:dateFor];
    self.stopwatchLabel.text = timeString;
}

- (IBAction)onStartPressed:(id)sender {

NSString *isItEqual = self.stopwatchLabel.text;

if ([isItEqual isEqualToString:@"00:00:00"]) {
    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];
}
else {
    // Create the stop watch timer that fires every 10 ms
    self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                           target:self
                                                         selector:@selector(updateResumeTimer)
                                                         userInfo:nil
                                                          repeats:YES];

}
}


- (IBAction)onStopPressed:(id)sender {
resumeText = self.stopwatchLabel.text;
 NSLog(@"Timer is %@", resumeText);

dateFormat=[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HH:mm:ss"];
dateFor=[dateFormat dateFromString:resumeText];
[dateFormat setDateFormat:@"HH:mm:ss"];

[self.stopWatchTimer invalidate];
self.stopWatchTimer = nil;

[self updateTimer];
}
Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top