我正在努力更新 UILabel 以倒计时,因为我使用以下代码
- (void)updateLabel {
// convert date string to date then set to a label
NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
[dateStringParser setDateFormat:@"MM/dd/yyyy"];
NSLog(@"date from update label %@", _selectedBirthdate);
//OUTPUT date from update label 07/23/2013
NSDate *date = [dateStringParser dateFromString:_selectedBirthdate];
NSLog(@"date from update label %@", date);
NSTimeInterval timeInterval = [date timeIntervalSinceNow]; ///< Assuming this is in the future for now.
NSString *stringVariable = [self stringFromTimeInterval:timeInterval];
self.dayLable.text = [NSString stringWithFormat:@"%@", stringVariable];
self.hourLable.text = [NSString stringWithFormat:@"%@", stringVariable];
self.minutesLable.text = [NSString stringWithFormat:@"%@", stringVariable];
// i want to update this day hour minutes lable like this much of days hour and minutes remaining
NSLog(@"%@",stringVariable);
}
我使用以下方法计算天数小时和分钟,但我不知道逻辑思维在这里不起作用如何从间隔中找到天小时和分钟
- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
NSInteger ti = (NSInteger)interval;
NSLog(@"%d",ti);
// NSInteger seconds = ti % 60;
NSInteger days = (ti * 60);
NSInteger minutes = (ti / 60) % 60;
NSLog(@"%d",minutes);
NSInteger hours = (ti / 3600);
NSLog(@"%d",hours);
return [NSString stringWithFormat:@"%02i hours : %02i min", hours, minutes];
}