当日期选择器模式设置为 时,您必须使用该countDownDuration
属性。UIDatePicker
UIDatePickerModeCountDownTimer
倒计时持续时间以秒为单位,因此您可以从 NSDate 计算如下(要对其进行测试,只需将其放入viewDidLoad
any 中UIViewController
):
// Create a new date with the current time
NSDate *date = [NSDate new];
// Split up the date components
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:date];
NSInteger seconds = ([time hour] * 60 * 60) + ([time minute] * 60);
UIDatePicker *picker = [UIDatePicker new];
[picker setDatePickerMode:UIDatePickerModeCountDownTimer];
[picker setCountDownDuration:seconds];
[[self view] addSubview:picker];
因此,如果当前时间是 17:28,UIDatePicker
则会显示“17 小时 28 分钟”。只需将 NSDate 替换为从数据库中获得的 NSDate 即可,您应该进行排序:)