Yang Jie Domodomo
Yang Jie Domodomo

Reputation: 685

NSTimer time increment

I'm a beginner in obj-C for iOS platform and am trying to build a few simple project to build my foundation.

I have a button which increase the NSTimer time for the label, but when I use NSLog to log the time, it uses the value before time increment was implemented. I need to be able to log a updated time (after increment), as I require that value and am implementing more function into the IBAction after I solve this portion.

E.g at 15min I press, the NSLog will read it as "00:15:00.0" rather than "00:35:00.0".

- (IBAction)onSkipPressed:(id)sender {
    startDate = [startDate dateByAddingTimeInterval:-1200];
    NSLog(@"%@",self.timeLabel.text);
}

Any one know the reason for this issue? And how should I solve it such that NSLog will read it as "00:35:00.0" if I invoke this IBAction at 15min.

EDIT - The start button will start the timer and timeLabel will get the string. Sorry for missing out such a important detail. I don't think there are any other code in the project which is related to this functionality already. Thank you for pointing it out to me.

- (void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.S"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    timeLabel.text = timeString;   
}

my IBAction to fire the timer

- (IBAction)onStartPressed:(id)sender {
    startDate = [NSDate date];

    gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                 target:self
                                               selector:@selector(updateTimer)
                                               userInfo:nil
                                                repeats:YES];

    //hide start button and show timeLabel
    startButton.hidden=true;
    timeLabel.hidden=false;

}

Upvotes: 0

Views: 891

Answers (2)

Yang Jie Domodomo
Yang Jie Domodomo

Reputation: 685

I went back to do a few revision with tutorials involving NSTimer. And turns out all I was missing was 1 line [self updateTimer]

- (IBAction)onSkipPressed:(id)sender {
    startDate = [startDate dateByAddingTimeInterval:-1200];
    [self updateTimer];
    NSLog(@"%@",self.timeLabel.text);
}

This solve my issue and the timeLabel.text is updated for me to log the information.

Upvotes: 1

Zhang
Zhang

Reputation: 11607

Um, why are you passing in negative 1200?

// this subtracts 1200 seconds from your date, no?
startDate = [startDate dateByAddingTimeInterval:-1200];

Shouldn't you do:

// add 30 minutes (60 seconds a minute x 30 minutes) to your time interval
startDate = [startDate dateByAddingTimeInterval:(60 * 30)];
...
NSLog(@"%@",self.timeLabel.text);

Or am I misunderstanding something?

Upvotes: 0

Related Questions