点击日期按钮,弹出一个日期选择框在底部,选完日期后,修改日期按钮上的文字:
关于日期按钮的外观绘制详见上一节,点击按钮的响应事件为:
@IBAction func selectDate(sender: AnyObject) { let alertController:UIAlertController=UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n\n\n",message: nil,preferredStyle: UIAlertControllerStyle.ActionSheet) // 初始化 datePicker let datePicker = UIDatePicker( ) //将日期选择器区域设置为中文,则选择器日期显示为中文 datePicker.locale = NSLocale(localeIdentifier: "zh_CN") // 设置样式,当前设为同时显示日期和时间 datePicker.datePickerMode = UIDatePickerMode.Date // 设置默认时间 datePicker.date = NSDate() // 响应事件(只要滚轮变化就会触发) // datePicker.addTarget(self,action:Selector("datePickerValueChange:"),forControlEvents: UIControlEvents.ValueChanged) alertController.addAction(UIAlertAction(title: "确定",style: UIAlertActionStyle.Default){ (alertAction)->Void in print("date select: \(datePicker.date.description)") //获取上一节中自定义的按钮外观DateButton类,设置DateButton类属性thedate let myDateButton=self.Datebutt as? DateButton myDateButton?.thedate=datePicker.date //强制刷新 myDateButton?.setNeedsDisplay() }) alertController.addAction(UIAlertAction(title: "取消",style: UIAlertActionStyle.Cancel,handler:nil)) alertController.view.addSubview(datePicker) self.presentViewController(alertController,animated: true,completion: nil) }原文链接:/swift/325802.html