我試着用NSTimeInterval進行倒計時。但我希望能夠在不每次發佈更新的情況下更改間隔。所以我試圖從我的網站導入時間間隔。我已經將NSTimeInterval的數字存儲在一個NSString中,並且現在想要將它們轉換爲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沒有做任何事情......
什麼問題?它似乎好 – 2012-02-07 17:57:29
它不工作?您是否驗證過(通過顯示或查看調試器)字符串值是否正確進入?您是否驗證過標籤文字設置正確? – 2012-02-07 17:58:40
什麼顯示label.text? – Shmidt 2012-02-07 17:58:40