4

How can I change this code so it has HH:MM:SS (hours, minutes, seconds)?

Do I need to add code in .h or .m so I known which one?

At the moment it goes up like 1, 2, 3, 4 etc.

Just to let you known I'm bait of a amateur would you copy and past so I known what you mean.

.h

@interface FirstViewController : UIViewController {
    
    IBOutlet UILabel *time; 
    
    NSTimer *myticker;
    
    //declare baseDate
    NSDate* baseDate; 

}

-(IBAction)stop;
-(IBAction)reset;

@end

.m

#import "FirstViewController.h"

@implementation FirstViewController

-(IBAction)start {
    [myticker invalidate];
    baseDate = [NSDate date];
    myticker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}

-(IBAction)stop;{ 
    
    [myticker invalidate];
    myticker = nil;
}
-(IBAction)reset;{
    
    time.text = @"00:00:00";
}
-(void)showActivity {
    NSTimeInterval interval = [baseDate timeIntervalSinceNow];
    NSUInteger seconds = ABS((int)interval);
    NSUInteger minutes = seconds/60;
    NSUInteger hours = minutes/60;
    time.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes%60, seconds%60];
}
peterh
  • 11,875
  • 18
  • 85
  • 108
Paul Mastes
  • 71
  • 2
  • 7

1 Answers1

7

First, declare baseDate variable in your FirstViewController.h, like this:

@interface FirstViewController : UIViewController {

    IBOutlet UILabel *time; 

    NSTimer *myticker;

    //declare baseDate
    NSDate* baseDate;
}

Then, in the FirstViewController.m start method add baseDate = [NSDate date] like this:

-(IBAction)start {
    [myticker invalidate];
    baseDate = [NSDate date];
    myticker = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}

After that, change your showActivity method to look like this:

-(void)showActivity {
    NSTimeInterval interval = [baseDate timeIntervalSinceNow];
    double intpart;
    double fractional = modf(interval, &intpart);
    NSUInteger hundredth = ABS((int)(fractional*100));
    NSUInteger seconds = ABS((int)interval);
    NSUInteger minutes = seconds/60;
    NSUInteger hours = minutes/60;
    time.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", hours, minutes%60, seconds%60, hundredth];
}

Also note, that you will have to change the interval value for your timer, otherwise your label will only be updated once a second.

lawicko
  • 7,246
  • 3
  • 37
  • 49
  • hi would you be able to add it in my code above because im finding this hard to understand ?? declare basedate variable ???? thank you – Paul Mastes Feb 13 '12 at 13:57
  • Which step do you find hard to understand? – lawicko Feb 13 '12 at 14:00
  • where a bouts do i put the baseDate = [NSDate date]; in my code and -(void)showActivity { NSTimeInterval interval = [baseDate timeIntervalSinceNow]; NSUInteger seconds = ABS((int)interval); NSUInteger minutes = seconds/60; NSUInteger hours = minutes/60; time.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes%60, seconds%60]; } – Paul Mastes Feb 13 '12 at 14:01
  • in the .h the is a error it says interface type cannot be statically allocated next to NSDate baseDate; any ideas ?? thanks for this so far mate you the best help on here – Paul Mastes Feb 13 '12 at 14:19
  • hi mate this has now worked this is great could i ask 1 more thing if i wanted this to count up in MM:SS:Ns nano seconds do you known or ?? – Paul Mastes Feb 13 '12 at 14:26
  • like the one on the iPhone clock ?? – Paul Mastes Feb 13 '12 at 14:29
  • I have updated my answer showing how to get the desired precision. Note that you will have to change your time interval for the timer accordingly, otherwise your label will only be updated once a second. – lawicko Feb 13 '12 at 14:56
  • hi mate thats everything in my .h and .m have i done it all right i haven't added the last code you did because i don't known where to put it i did it put it come up with errors ?? – Paul Mastes Feb 13 '12 at 16:20
  • I've updated my answer, two things must be changed - the timer interval to 0.01 and the *showActivity* method. I hope it will finally do exactly what you want ;-) – lawicko Feb 13 '12 at 16:36
  • yeahhhhhh thats perfect mate your have to be the best person on here thanks buddy :) – Paul Mastes Feb 13 '12 at 16:40
  • here there lawicko do you have a email so i can contact you or if you get this do you known what i could add to stop this error in the video^^^^ – Paul Mastes Feb 14 '12 at 14:40