Search code examples
iphoneobjective-cxcodecountdown

iPhone : Countdown problems (00:00:00 format)


I use a UISlider to get the input in minutes (range 1 - 120) for a countdown timer and show it on a label like this "01:30:00". i) I would like when the user sets the timer (using the slider) to adjust the hours and minutes but NOT the seconds. The seconds should start counting AFTER the user starts the countdown. How can i do that? ii) i have trouble updating the countdownlabel. Could somebody suggest the correct code?

-(IBAction)setTime:(id)sender {
totaltime=timeSlider.value;
hours = totaltime / 60;
minutes = (totaltime % 3600) % 60;
seconds = (totaltime % 3600) * 60;
[countDownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:45]];
countDownLabel.text = [NSString stringWithFormat:@"%.2i:%.2i:%.2i", hours, minutes, seconds]; }

 -(void)countdown {
 totaltime -=1; 


if(minutes == 0) { [timer invalidate]; }    
 [countDownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:45]];
 countDownLabel.text = [NSString stringWithFormat:@"%.2i:%.2i:%.2i", hours, minutes, seconds]; }
-(IBAction)fade {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                        target:self selector:@selector (countdown)  userInfo:nil repeats:YES]; }

Solution

  • -(IBAction)setTime:(id)sender {
    totaltime=timeSlider.value;
    hours = totaltime / 60;
    minutes = (totaltime % 3600) % 60;
    seconds = (totaltime % 3600) * 60;
    [countDownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:45]];
    countDownLabel.text = [NSString stringWithFormat:@"%.2i:%.2i:%.2i", hours, minutes, seconds]; }
    
     -(void)countdown {
     totaltime -=1; 
    totaltime=timeSlider.value;
    hours = totaltime / 60;
    minutes = (totaltime % 3600) % 60;
    seconds = (totaltime % 3600) * 60;
    countDownLabel.text = [NSString stringWithFormat:@"%.2i:%.2i:%.2i", hours, minutes, seconds];
    
    
    if(minutes == 0) { [timer invalidate]; }    
     [countDownLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:45]];
     countDownLabel.text = [NSString stringWithFormat:@"%.2i:%.2i:%.2i", hours, minutes, seconds]; }
    -(IBAction)fade {
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                            target:self selector:@selector (countdown)  userInfo:nil repeats:YES]; }