Wednesday 29 May 2013

Watch or counter with NSTimer

take object of NSTimer and also int variable  in .h file like bellow..

    NSTimer *timer;
    int hours, minutes, seconds;
    int secondsLeft;

and just use it with bellow method

- (void)updateCounter:(NSTimer *)theTimer {
    secondsLeft++;
    hours = secondsLeft / 3600;
    minutes = (secondsLeft % 3600) / 60;
    seconds = (secondsLeft %3600) % 60;
    //lblTime.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    NSLog(@"\n%02d:%02d:%02d",hours,minutes,seconds);

}

-(void)countdownTimer{
    
    if([timer isValid])
    {
        [timer release];
    }
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    [pool release];
}

See My Answer also converstion-of-seconds-in-minutes