当用户选择日期时,我在尝试让UIDatePicker工作时遇到问题。
我一直收到错误说:无法识别的选择器发送到实例。
func textFieldDidBeginEditing(_ textField: UITextField) {
let datePicker: UIDatePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.date
textField.inputView = datePicker
datePicker.backgroundColor = UIColor.white
datePicker.addTarget(self, action: Selector(("datePickerChanged:")), for: .valueChanged)
}
func datePickerChanged(sender: UIDatePicker){
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.medium
DEPtextField.text = formatter.string(from: sender.date)
}
答案 0 :(得分:0)
DatePicker拥有响应更改事件的属性。 https://developer.apple.com/reference/uikit/uicontrolevents/1618238-valuechanged
答案 1 :(得分:0)
如下更改功能
func datePickerChanged(_ sender: UIDatePicker){
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.medium
DEPtextField.text = formatter.string(from: sender.date)
}
应该可以。