问题是停止 NSTimer,由于某种原因 [Timer invalidate] 只是不工作......
可能我的眼睛里全是肥皂,但是不明白为什么定时器没有停在0,而是倒数-1、-2、-3等等……(((
我使用纪元数字作为目的地日期。还有一件事-我的带有[Timer invalidate]的按钮“IBAction stop”工作得很好-当我在模拟器中按下它时计时器停止...
@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;
}
}