0

When I click on Start the stopWatchLabel shows the following (its static, nothings runs):

  • NOTE: When I test this app on my iPhone all runs as expected. No problems at all.

Can someone explain why?

enter image description here

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSTimer *stopWatchTimer; 
    NSDate *startDate;
}


@property (strong, nonatomic) IBOutlet UILabel *stopWatchLabel;

- (IBAction)startButtonTapped:(id)sender;

- (IBAction)stopButtonTapped:(id)sender;

-(void)updateTimer;

@end

.m

- (IBAction)startButtonTapped:(id)sender {

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                      target:self 
                                                    selector:@selector(updateTimer) 
                                                    userInfo:nil 
                                                     repeats:YES];
}


- (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"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;
}


- (IBAction)stopButtonTapped:(id)sender {

    [stopWatchTimer invalidate];
}
  • 1
    See [here](http://stackoverflow.com/questions/1189252/how-to-convert-an-nstimeinterval-seconds-into-minutes) for getting the hours and minutes directly from the `NSTimeInterval`. No need to create a new date and format it. – Joe Mar 29 '12 at 13:12

1 Answers1

0

you don't set startDate

your code should look like this (I assume this is ARC):

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

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                      target:self 
                                                    selector:@selector(updateTimer) 
                                                    userInfo:nil 
                                                     repeats:YES];
}

and the updateTimer is a ridiculous complicated method to split seconds into minutes and seconds. Do some basic math.

- (void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];

    NSInteger minutes = floor(timeInterval/60);
    NSInteger seconds = trunc(timeInterval - minutes * 60);

    NSString *timeString=[NSString stringWithFormat:@"%i:%02i", minutes, seconds];
    stopWatchLabel.text = timeString;
}

EDIT: Actually you should use the built in NSCalendar/NSDateComponents API because it does not ignore daylight saving like the basic math method.

- (void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSSecondCalendarUnit|NSMinuteCalendarUnit fromDate:startDate toDate:currentDate options:0];
    NSInteger minutes = [components minute];
    NSInteger seconds = [components second];

    NSString *timeString=[NSString stringWithFormat:@"%i:%02i", minutes, seconds];
    stopWatchLabel.text = timeString;
 }
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247