UIDatePickerModeCountDownTimer模式下的UIDatePicker:如何通过代码获取/设置小时和分钟? [英] UIDatePicker in UIDatePickerModeCountDownTimer mode: how to get/set the hours and minutes via code?

查看:185
本文介绍了UIDatePickerModeCountDownTimer模式下的UIDatePicker:如何通过代码获取/设置小时和分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过代码从UIDatePickerModeCountDownTimer模式下的UIDatePicker获取/设置小时和分钟?

How to get/set the hours and minutes via code from a UIDatePicker in UIDatePickerModeCountDownTimer mode?

情况:

  • 我有一个界面,用户可以选择几个小时和几分钟.然后,他保存了信息(因此,我必须通过代码从UIDatePicker中获取小时和分钟,以保存到数据库中.)

  • I have an interface in that a user selects just hours and minutes. Then, he saves the information (so, I have to get the hours and minutes from UIDatePicker via code to save in a DB).

当用户编辑以前保存的信息时,我想使用保存的小时/分钟启动界面(因此,我必须通过代码和DB中的值来设置UIDatePicker的小时和分钟).

When the user is editing the information saved previously, I want to start the interface with the saved hours/minutes (so, I have to set the UIDatePicker hours and minutes via code with values from DB).

在UIDatePickerModeCountDownTimer模式下如何使用UIDatePicker进行操作?

How do I do that with the UIDatePicker in UIDatePickerModeCountDownTimer mode?

谢谢.

推荐答案

当日期选择器模式设置为UIDatePickerModeCountDownTimer时,必须在UIDatePicker中使用countDownDuration属性.

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

倒计时持续时间以秒为单位,因此您可以按照以下方式从NSDate计算该时间(要进行测试,只需将其放入任何UIViewControllerviewDidLoad中):

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];

因此,如果当前时间是17:28,则UIDatePicker将显示"17小时28分钟".将NSDate替换为从数据库中获得的NSDate,应该对它进行排序:)

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 :)

这篇关于UIDatePickerModeCountDownTimer模式下的UIDatePicker:如何通过代码获取/设置小时和分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆