【发布时间】:2015-05-10 15:04:30
【问题描述】:
首先,这是我不想达到的目标:
我想从Current date and Time 运行timer 到user Selected Date and Time。
我做了什么:
- 来自
DatePicker的IBOutlet - 做了一个
UIButton
我尝试过的:
- (IBAction)schedulePostTapped:(id)sender
{
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountdown:) userInfo:nil repeats:YES];
}
- (void)updateCountdown:(NSTimer *)timer
{
NSDate *startingDate = [chooseDate date];
NSDate *endingDate = [NSDate date];
double secondsTime = [endingDate timeIntervalSinceDate:startingDate];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];
NSInteger hours = [dateComponents hour];
NSInteger minutes = [dateComponents minute];
NSInteger seconds = [dateComponents second];
NSString *countdownText = [NSString stringWithFormat:@"%ld Hours %ld Minutes %ld Seconds",(long)hours, (long)minutes, (long)seconds];
self.statusLabel.text = countdownText;
NSLog(@"%@",[NSString stringWithFormat:@"%f",secondsTime]);
if (secondsTime==0) {
[timer invalidate];
timer = nil;
}
}
我遇到的问题:
- 每次我选择一个时间,即使比当前时间晚一分钟,计时器也会随机增加一些额外的秒数。我想不通。
** 延迟必须是 1m04Sec,但它显示的是 1m55Sec**
【问题讨论】:
-
@Zaph 看到我用 LINK 更新了我的问题
-
NSLog 在获得结束日期后立即记录
startingDate和endingDate。手动计算差异。另请注意,时间间隔以秒为单位,而不是毫秒。
标签: ios xcode6 nsdate nstimer uidatepicker