let alertController:UIAlertController=UIAlertController(title:"\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
// 初始化 datePicker
let datePicker =UIDatePicker( )
//将日期选择器区域设置为中文,则选择器日期显示为中文
datePicker.locale =NSLocale(localeIdentifier:"zh_CN") asLocale
// 设置样式,当前设为同时显示日期和时间
datePicker.datePickerMode =UIDatePickerMode.date
// 设置默认时间
datePicker.date =NSDate()asDate
// 响应事件(只要滚轮变化就会触发)
// datePicker.addTarget(self, action:Selector("datePickerValueChange:"), forControlEvents: UIControlEvents.ValueChanged)
alertController.addAction(UIAlertAction(title:"确定", style:UIAlertActionStyle.default){
(alertAction)->Voidin
//更新提醒时间文本框
let formatter =DateFormatter()
//日期样式
formatter.dateFormat ="yyyy-MM-dd"
self.labStartTime.text=formatter.string(from: datePicker.date)
})
alertController.addAction(UIAlertAction(title:"取消", style:UIAlertActionStyle.cancel,handler:nil))
alertController.view.addSubview(datePicker)
self.present(alertController, animated:true, completion:nil)
方式二:使用ActionSheetPicker-3.0
https://github.com/skywinder/ActionSheetPicker-3.0#cocoapods
let picker=ActionSheetDatePicker(title:"", datePickerMode:UIDatePickerMode.date, selectedDate:NSDate()asDate!, doneBlock: { picker, value, indexin
//更新提醒时间文本框
let formatter =DateFormatter()
//日期样式
formatter.dateFormat ="yyyy-MM-dd"
self.labStartTime.text=formatter.string(from: value as! Date)
return
}, cancel: { ActionStringCancelBlock in return }, origin:self.viewStartTime)
picker?.setDoneButton(UIBarButtonItem(title:"确认", style:UIBarButtonItemStyle.plain, target:self, action:nil))
picker?.setCancelButton(UIBarButtonItem(title:"取消", style:UIBarButtonItemStyle.plain, target:self, action:nil))
picker?.show()