Because your currentDate
will keep being set every time your timer ticks. [NSDate date]
will set currentDate
to the current time every time the updateCountdown
method runs. Therefore will finalTime
always be 45 minutes ahead of currentDate
. You should create a startDate
property and set it in the start
method instead:
- (void)start
{
self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
self.startDate = [NSDate date];
}
Then check on the property in the updateCountdown
method:
if ([self.startDate compare:finalTime] == NSOrderedSame)
{
NSLog(@"Done.");
[self stop];
}
Alternatively you can use an integer with the number of ticks you are expecting and then substract one from the integer everytime the timer ticks. Something like this:
- (void)start
{
self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
self.countdown = TIMER_COUNT * HOURS_IN_HOUR;
}
- (void)updateCountdown
{
self.countdown--;
//your code
if (self.countdown == 0)
{
NSLog(@"Done.");
[self stop];
}
}