Note that there are some explanatory texts on larger screens.

plurals
  1. PONSTimer time increment
    text
    copied!<p>I'm a beginner in obj-C for iOS platform and am trying to build a few simple project to build my foundation.</p> <p>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.</p> <p>E.g at 15min I press, the NSLog will read it as "00:15:00.0" rather than "00:35:00.0".</p> <pre><code>- (IBAction)onSkipPressed:(id)sender { startDate = [startDate dateByAddingTimeInterval:-1200]; NSLog(@"%@",self.timeLabel.text); } </code></pre> <p>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. </p> <p>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.</p> <pre><code>- (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; } </code></pre> <p>my IBAction to fire the timer</p> <pre><code>- (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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload