Search code examples
iphoneobjective-ctimeruilabelupdating

How to make a UILabel that constantly updates in Objective-C


I'm looking to make a UILabel display the current progress of a timer. Right now to get the current time left I call [timer1 timeLeft]; which returns an int. In this way I can update the label ONCE, at one instant. In what way can I update the label (mainLabel) constantly so that it is always displaying the current timer progress while being somewhat resource efficient?

Thanks for all your help!


Solution

  • Use following code for Countdown timer.

    dblElapsedSeconds=0.0; //Declare this in header
    tmrElapsedTime = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateElapsedTime) userInfo:nil repeats:YES]; //Declare timer variable in header
    
    -(void)updateElapsedTime
    {
        dblElapsedSeconds += 1;
        //double seconds = [[NSDate date] timeIntervalSinceDate:self.startTime];
        int hours,minutes, lseconds;
        hours = dblElapsedSeconds / 3600;
        minutes = (dblElapsedSeconds - (hours*3600)) / 60;
        lseconds = fmod(dblElapsedSeconds, 60); 
        [lblTimeElapsed setText:[NSString stringWithFormat:@"%02d:%02d",minutes, lseconds]];
    }