说明:
UIDatePicker是系统自带的时间选择控件
使用:
1.属性介绍:
将UIDatePicker控件拖动到Main.storyboard上,选中此控件时,右侧属性面板可以选择中英文,显示模式等:
Mode:
Date and Time:显示日期+时间
Date:只显示日期
Time:只显示时间
Count Down Timer:显示倒计时
Locale:选择Chinese(Simplified)为简体中文显示
2.给UIDatePicker增加滚动事件监听:
- (void)viewDidLoad {
[super viewDidLoad];
//监听DataPicker的滚动,onDateChange为自定义方法
[self.datePicker addTarget:self action:@selector(onDateChange:) forControlEvents:UIControlEventValueChanged];
}
/*
自定义方法,接收DataPicker滚动事件
*/
- (IBAction)onDateChange:(UIDatePicker *)datePicker {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
//设置时间格式
format.dateFormat = @"yyyy年MM月dd日 hh:mm:ss";
NSString *dateStr = [format stringFromDate:datePicker.date];
//将当前的时间显示到UILabel上
self.dateTv.text = dateStr;
}