1

In my app , i let user to set Count Down timer with UIDatePicker's CountDownTimer.

I have one UILabel to show count time When user choose time from CountDownTimer, i want to show coumt down that user chosen in CoutDownTimer like following pic.

enter image description here

So i retrieve count time that user chosen from CountDownTimer with following code

self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setDateFormat:@"HH:mm:ss"];

self.sleepTimer = [NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];

When it reach 00:00 , i want to fire a event.

I don't know how to reduce every second count down.

Thanks you for your help.

Fire Fist
  • 7,032
  • 12
  • 63
  • 109

3 Answers3

3

You can do it by :

[NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(decrementByOne:) userInfo:nil repeats:YES];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • i don't know how to reduce 1 for NSDateFormatter.plz help. – Fire Fist Feb 26 '13 at 13:37
  • I didn't talk for dateFormatter. I said make a counter that will be counted per second from starting. Then the above selector will call it per second and your counter will be decrementedByOne and display it. – Anoop Vaidya Feb 26 '13 at 13:42
2

Save the countDownDuration property of your UIDatePicker (the value is given in seconds) in an instance variable called something like secondsLeft.

self.sleepTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

That will call your method timerFired: every second. Subtract secondsLeft by 1. Update your UILabel with the time left (format your seconds into hh:mm:ss - See this question on formatting). Validate if there is time left on the count down and take the appropriate action if the count down is complete (secondsLeft <= 0).

Remember to clean up once the countdown is complete;

[self.sleepTimer invalidate];
self.sleepTimer = nil;
Community
  • 1
  • 1
Mattias
  • 2,266
  • 2
  • 15
  • 16
0

Take your date, save it into a property and take a look at NSDates

– dateByAddingTimeInterval:

So if you do

[self.date dateByAddingTimeInterval:-1];

you will decrement your date by 1 Second. After that, update you UI with the new calculated date.

Further you could package all the code in one function and execute this function by calling:

performSelector:withObject:afterDelay: 
Alexander
  • 7,178
  • 8
  • 45
  • 75