我有多个UITextField
,其中一些会使用' UIDatePicker'。如何在不必在textField.text = formatter.stringFromDate(datePicker.date)
函数中编写datePickerChanged
代码的情况下从UIDatePicker返回值?
func textFieldDidBeginEditing(textField: UITextField) {
switch textField.tag {
//tag = 3 is the text field that needs the date
case 3:
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.addTarget(self, action: "datePickerChanged:", forControlEvents: .ValueChanged)
//textField.text = value returned from datePickerChanged
default: break
}
}
func datePickerChanged(datePicker: UIDatePicker) {
let formatter = NSDateFormatter()
formatter.dateStyle = .MediumStyle
let dateTextValue = formatter.stringFromDate(datePicker.date)
// how can I return (dateTextValue)?
}
答案 0 :(得分:1)
您可以创建一个名为currentTextField
的变量,并在您的案例陈述中将textField
分配给currentTextField
。然后你的datePickerChanged(_ :)函数可以使用currentTextField
来设置日期。