0

Please help,

I have a NSTimeInterval which I convert into a NSDateComponent and then assign each component (year, month, day, etc) to an appropriate label. After I do the conversion I start an NSTimer to begin a countdown. Here's the code I use:

- (void)configureView
{
    if (self.detailItem) {
        self.progressView.progress = 0.0;
        self.initialInterval = [[self.detailItem valueForKey:@"date"] timeIntervalSinceNow];
        self.timeElapsed = self.initialInterval;
        self.secondsLeft = [self resetLabel];
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                          target:self
                                                        selector:@selector(timerFireMethod:)
                                                        userInfo:nil
                                                         repeats:YES];
    }
}

- (void)timerFireMethod:(NSTimer *)timer
{
    if (self.secondsLeft > 0) {
        self.secondsLeft--;
        self.timeElapsed--;
        [self updateSecondsWithNumber:self.secondsLeft];
        self.progressView.progress = 1 - (self.timeElapsed / self.initialInterval);
    } else {
        if (self.timeElapsed <= 0) { [timer invalidate]; return; }
        [self resetLabel];
        self.secondsLeft = 59;
    }
}

- (void)updateSecondsWithNumber:(int)number
{
    self.secondLabel.text = [NSString stringWithFormat:@"%ds", number];
}

- (NSTimeInterval)resetLabel
{
    NSDateComponents *convertedInfo = [ChronoModel dateComponentFromTimeInterval:self.timeElapsed];
    self.yearLabel.text = [NSString stringWithFormat:@"%ldy", (long)convertedInfo.year];
    self.monthLabel.text = [NSString stringWithFormat:@"%ldm", (long)convertedInfo.month];
    self.dayLabel.text = [NSString stringWithFormat:@"%ldd", (long)convertedInfo.day];
    self.hourLabel.text = [NSString stringWithFormat:@"%ldh", (long)convertedInfo.hour];
    self.minuteLabel.text = [NSString stringWithFormat:@"%ldm", (long)convertedInfo.minute];
    self.secondLabel.text = [NSString stringWithFormat:@"%lds", (long)convertedInfo.second];
    return convertedInfo.second;
}

timeElapsed, initialInterval and secondsLeft are all NSTimeIntervals.

For some reason the label jumps two seconds instead of just one. I have no idea why this is happening. Any ideas?

Thank you.

user1563544
  • 379
  • 3
  • 17
  • Add an `NSLog( @"hello" );` as the first line of the `timerFireMethod` and let's see what you get. – user3386109 Apr 29 '14 at 00:57
  • I added an NSLog to see the values of secondsLeft and timeElapsed and it seems that they decrement well. But each line of NSLog appears two at a time every second. I wish I knew the reason :D – user1563544 Apr 29 '14 at 01:07
  • My best guess is that you have two instances of the timer running. Try putting an NSLog ahead of the `[NSTimer scheduledTimerWithTimeInterval` line. – user3386109 Apr 29 '14 at 01:10
  • Your best guess was the right one :) Thank you sir! The code provided by Apple which includes configureView is called twice, so it wasn't the best place to put my timer. If you answer my question, I'll mark it as the correct one. – user1563544 Apr 29 '14 at 01:15

1 Answers1

1

My best guess is that you have two instances of the timer running. Try putting an NSLog ahead of the [NSTimer scheduledTimerWithTimeInterval line.

user3386109
  • 34,287
  • 7
  • 49
  • 68