Problem is to stop NSTimer, for some reason [Timer invalidate] just not working...
Maybe my eyes are full of soap, but can't understand the reason why the timer didn't stop at 0, but go reverse counting -1, -2, -3 and so on...(((
I'm using epoch numbers as destination date. One more thing - my button "IBAction stop" with [Timer invalidate] works just fine - when i push it in simulator timer stops...
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
- (IBAction) start {
Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
- (IBAction) stop {
[Timer invalidate];
Timer = nil;
}
-(void)updateLabel {
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calender components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c", [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']];
destinationDate = [NSDate dateWithTimeIntervalSince1970:1355299710];
if (!destinationDate) {
[Timer invalidate];
Timer = nil;
}
}