我试图用 NSTimeInterval 倒计时。但我希望能够更改间隔而无需每次都发布更新。所以我尝试从我的网站导入 Timeinterval。我已经将 NSTimeInterval 的数字存储在 NSString 中,现在想将它们转换为 NSTimeInterval 以便将其实现到倒计时代码中......
...但它不工作。有任何想法吗?
label.text = string;
double timeInterval = [label.text doubleValue];
NSTimeInterval intervalForTimer = timeInterval;
destinationDate = [[NSDate dateWithTimeIntervalSince1970:intervalForTimer] retain];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
编辑:
- (void)updateLabel {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components minute], 'm', [components second], 's']];
}
我的新代码如下所示:
NSLog(@"Downloaded file with contents: %@", string);
double timeInterval = [string doubleValue];
label.text = [NSString stringWithFormat:@"%d", (int)timeInterval];
NSTimeInterval intervalForTimer = timeInterval;
destinationDate = [[NSDate dateWithTimeIntervalSince1970:timeInterval] retain];
timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
但是 dateLabel 没有做任何事情......