一、介绍
UIDatePicker使用多个可以旋转的轮子,允许用户去选择日期和时间。iPhone本身提供一个闹钟的应用,可以用来定制闹钟和定时。你也可以使用UIDatePicker用来实现一个倒计时。当用户完成一个旋转操作改变日起或者时间后,UIDatePicker会发送一个动作消息,与此关联的控制事件是UIControlEventValueChanged.UIDatePicker存在一个定时器,但是并没有实现;应用必须设置一个NSTimer对象并且当计数减少时更新秒数。UIDatePicker并不继承自UIPIckerView,但它管理一个自定义的picker-view 当作子视图。
二、相关属性和方法
1、管理日期和日历
@property(nonatomic, copy) NSCalendar * __null_unspecified calendar 给date picker使用的日历
@property(nonatomic, strong) NSDate *date date picker显示的日期
@property(nonatomic, strong) NSLocale *locale UIDatePicker使用的地址
- (void)setDate:(NSDate * nonnull)date animated:(BOOL)animated 设置日期及是否需要动画
@property(nonatomic, strong) NSTimeZone *timeZone
2、配置UIDatePicker的模式
@property(nonatomic) UIDatePickerMode datePickerMode
3、配置时间属性
@property(nonatomic, strong) NSDate *maximumDate
@property(nonatomic, strong) NSDate *minimumDate
@property(nonatomic) NSInteger minuteInterval 分钟选择的间隔
@property(nonatomic) NSTimeInterval countDownDuration
4、常量
typedef enum {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer
} UIDatePickerMode;
三、实际例子
-(void)createDatePicker{
UIDatePicker *dataPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(20, 84, 300, 80)];
dataPicker.datePickerMode = UIDatePickerModeCountDownTimer;
[dataPicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:dataPicker];
dataPicker.countDownDuration = 30.00;
dataPicker.minuteInterval = 5;
}
-(void)dateChanged:(id)sender{
UIDatePicker *picker = (UIDatePicker *)sender;
NSDate *date = picker.date;
}