0

I am trying to get a timer to show the counter like this "00:00:00". Here is my current code. I have been trying to get it to work using the stringWithFormat which should be easy but I guess I will have to set up the formats separately. Do you guys have any idea on how to do this?

- (void)TimerCount {
    CountNumber = CountNumber + 1;
    TimerDisplay.text = [NSString stringWithFormat:@"Hour: %0*i", length, hour];
}
Wain
  • 118,658
  • 15
  • 128
  • 151
Weeb
  • 11
  • 2
  • Your code doesn't match your request (format). You want hours, minutes and seconds presumably? Where are you calculating them? What is `length` and `hour` (int / float)? – Wain Apr 03 '14 at 09:22
  • I have set them up in the .h file but I actually think this works better: – Weeb Apr 03 '14 at 09:24
  • '-(void)TimerCount{ CountNumber = CountNumber + 1; TimerDisplay.text = [NSString stringWithFormat:@"%.4d", CountNumber]; }' – Weeb Apr 03 '14 at 09:25
  • Kind of messy but I hope you understand what I mean. Do you have an idea on how to set up the hours, minutes and seconds? – Weeb Apr 03 '14 at 09:26
  • You want a timer counting up, from zero when started? And the timer fires each second? – Wain Apr 03 '14 at 09:29
  • The timer starts when a button is tapped. I have a start, stop and reset button which all work great. – Weeb Apr 03 '14 at 09:29
  • I just need the label (which shows the hours, minutes and seconds) to be able to show them in this format "00:00:00" – Weeb Apr 03 '14 at 09:30
  • It now looks like this "0:00" with this code: – Weeb Apr 03 '14 at 10:27
  • .h file: 'int hours; int minutes; int seconds;' – Weeb Apr 03 '14 at 10:28
  • .m file: '-(void)TimerCount{ CountNumber = CountNumber + 1; TimerDisplay.text = [NSString stringWithFormat:@"%d:%02d", hours, minutes, seconds]; } – Weeb Apr 03 '14 at 10:29

2 Answers2

1
- (void)timerCount {

 {
    CountNumber = CountNumber + 1;
    NSInteger seconds = CountNumber % 60;
    NSInteger minutes = (CountNumber / 60) % 60;
    NSInteger hours = (CountNumber / 3600);
    TimerDisplay.text =  [NSString stringWithFormat:@"%i:%02i:%02i", hours, minutes, seconds];
}

Try the above code.

And configure this method to be fired every second.

In viewDidLoad

NSTimer *counterTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                        target:self 
                                                      selector:@selector(timerCount) 
                                                      userInfo:nil 
                                                       repeats:YES];
[[NSRunLoop mainRunLoop] addTimer: counterTimer forMode:NSRunLoopCommonModes];

And keep the counterTimer as an iVar to keep it alive until the VC is dealloced, if you are using ARC.

Selvin
  • 12,333
  • 17
  • 59
  • 80
  • But I am using buttons please check out these two links. CodeFile is the .m file and AppCode2 is .h file. dropbox.com/s/euzcos4vz64q86q/CodeFile.docx dropbox.com/s/9vixxlsuq6iogla/AppCode2.zip – Weeb Apr 03 '14 at 12:35
  • Turns out it works anyway! :) – Weeb Apr 03 '14 at 12:40
  • The reason I mentioned the buttons is because I don't need the viewDidLoad code then but anyway it works! :) – Weeb Apr 03 '14 at 12:42
-1
- (void)TimerCount
{
    CountNumber++;
    NSString *time = [[NSString alloc] init];
    NSUInteger seconds = CountNumber;
    NSUInteger minutes = 0;
    NSUInteger hours = 0;
    if (seconds > 59) {
        seconds -= 60;
        minutes++;
        if (seconds < 10) {
            time = [NSString stringWithFormat:@":0%i",seconds];
        } else time = [NSString stringWithFormat:@":%i",seconds];
    }
    if (minutes > 59) {
        minutes -= 60;
        hours++;
        if (minutes < 10) {
            time = [NSString stringWithFormat:@":0%i%@",minutes,time];
        } else time = [NSString stringWithFormat:@":%i%@",minutes,time];
    }
    if (hours < 10) {
        time = [NSString stringWithFormat:@"0%i%@",hours,time];
    } else time = [NSString stringWithFormat:@"%i%@",hours,time];

}

NSString *time is the time.

Also NSTimer to call this method every second:

NSTimer *counterTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                        target:self 
                                                      selector:@selector(timerCount) 
                                                      userInfo:nil 
                                                       repeats:YES];
Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • I dont understand why I got a down vote for that. This is literally the answer. – Schemetrical Apr 03 '14 at 10:39
  • I don't know why either. I didn't down vote it. I will check out the code and see if it works. – Weeb Apr 03 '14 at 11:07
  • @Weeb Use NSTimer to call this method every second. – Schemetrical Apr 03 '14 at 11:11
  • I believe it works perfectly. Some of my other code just needs to fit this code. Here's the complete .m code: https://www.dropbox.com/s/euzcos4vz64q86q/CodeFile.docx – Weeb Apr 03 '14 at 11:15
  • The void TimerCount works now but I just need the other code to fit the new code in TimerCount. When I push the buttons now (Start, Stop and Reset) nothing happens but that is basically just an indicator that the TimerCount void works but the other code just need a bit of editing after the new code has been implemented into the TimerCount void. – Weeb Apr 03 '14 at 11:21
  • Here's the .h file: https://www.dropbox.com/s/9vixxlsuq6iogla/AppCode2.zip – Weeb Apr 03 '14 at 11:30