我尝试使用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并没有任何作用...