UIDatePicker in UIDatePickerModeCountDownTimer mode: how to get/set the hours and minutes via code?

纵然是瞬间 提交于 2019-12-01 00:42:26

You have to use the countDownDuration property in UIDatePicker when the date picker mode is set to UIDatePickerModeCountDownTimer.

The countdown duration is in seconds, so you can calculate that from a NSDate as follows (to test it just drop it into the viewDidLoad of 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];

So if the current time is 17:28, the UIDatePicker will show "17 hours 28 mins". Simly replace the NSDate with the one you get from the DB and you should be sorted :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!