27

I'm making an application where the exams is going on, so when exam start the, time should start with that. For example 30 minutes and it should reduce like 29:59.

How can I implement this?

Can anyone please give me a sample example or a easy step by step tutorial that i can follow?

1615903
  • 25,960
  • 9
  • 55
  • 82
zeeshan shaikh
  • 819
  • 3
  • 18
  • 33

1 Answers1

67

This code is used to create a countdown timer.

Code for .h file.

@interface UIMyContoller : UIViewController {

NSTimer *timer;
    IBOutlet UILabel *myCounterLabel;
}

@property (nonatomic, retain) UILabel *myCounterLabel;

-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;

@end

Code for .m file.

@implementation UIMyController
@synthesize myCounterLabel;

int hours, minutes, seconds;
int secondsLeft;

- (void)viewDidLoad {
    [super viewDidLoad];

    secondsLeft = 16925;
    [self countdownTimer];
}

- (void)updateCounter:(NSTimer *)theTimer {
    if(secondsLeft > 0 ) {
        secondsLeft -- ;
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;
        myCounterLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    } else {
        secondsLeft = 16925;
    }
}

-(void)countdownTimer {

    secondsLeft = hours = minutes = seconds = 0;
    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];
}

Hope this helps you out.

Stunner
  • 11,108
  • 12
  • 79
  • 138
Adrian P
  • 6,439
  • 4
  • 36
  • 55
  • Thank you so much its working :) – zeeshan shaikh Jun 18 '13 at 08:50
  • I think that if you are having a 30 minutes timer your screen will turn off and also your app will stop working. So if you go back turning on the screen to go to your app you will not countdown the pass time. – Camilo Aguilar Aug 07 '13 at 03:46
  • 1
    the problem with this is: if you use navigation controller, go back to previous view and reload this view, the seconds will go down two instead of one. – Kiddo Nov 09 '13 at 19:41
  • so i add [timer invalidate] onto the back button – Kiddo Nov 09 '13 at 19:50
  • 1
    This helped me a ton, thank you! For those who see this in the future: I was able to cut out a lot of this. In the last method I actually only needed the `timer = ...;` line-- everything else in `-(void)countdownTimer` I was able to safely omit. – temporary_user_name Dec 02 '14 at 22:41
  • It is not working on Scrolling Views, When I scroll down or up, timer get paused and start again when I release scrollview. – Dharmik Feb 12 '15 at 06:18
  • Seeing what Kiddo and Aerovistae said, you probably would want to add [timer invalidate] inside the if ([timer isValid]). Solves the problem while maintaining ARC compatibility. – Uzaak Mar 30 '15 at 15:02
  • I used this in a Today Extension. Working excellently! – Sjakelien Feb 18 '16 at 00:16
  • helped me a lot .....Works Perfect !!! – Abhishek PM Jun 15 '16 at 05:37
  • can you also give a hint for pausing the time? – Krutika Sonawala Sep 26 '17 at 08:38