我有一个文本字段,当它被选中时会弹出一个日期选择器而不是键盘,但它只会在第一次更新今天的日期。用户更改日期,但文本字段不会继续更新。有什么问题?
这就是我调用Datepicker并控制输入到文本字段的日期的方法:
@IBAction func dp(sender: UITextField) {
var DatePickerView : UIDatePicker = UIDatePicker()
DatePickerView.datePickerMode = UIDatePickerMode.Date
textfieldjobdate.inputView = DatePickerView
DatePickerView.addTarget(self, action: Selector("handleDatePicker"), forControlEvents: UIControlEvents.ValueChanged)
}
func handleDatePicker() {
var DatePickerView : UIDatePicker = UIDatePicker()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
textfieldjobdate.text = dateFormatter.stringFromDate(DatePickerView.date)
}
答案 0 :(得分:51)
您的handleDatePicker
方法每次调用时都会实例化一个新的DatePicker
。{}您需要向函数添加一个参数以传递发件人(在DatePicker
中创建的dp
)
@IBAction func dp(_ sender: UITextField) {
let datePickerView = UIDatePicker()
datePickerView.datePickerMode = .date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged)
}
@objc func handleDatePicker(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
textfieldjobdate.text = dateFormatter.string(from: sender.date)
}
答案 1 :(得分:1)
Swift 4.2版本
@IBAction func onEditDob(_ sender: UITextField) {
let datePickerView = UIDatePicker()
datePickerView.datePickerMode = .date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged)
}
@objc func handleDatePicker(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
dob.text = dateFormatter.string(from: sender.date)
}
答案 2 :(得分:-1)
let selectedDate = datePicker.date
print(selectedDate)
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
let strDate = formatter.string(from: selectedDate)
self.labelDate.text = strDate
答案 3 :(得分:-1)
datePicker = UIDatePicker(frame:CGRect(x: 0, y: self.view.frame.size.height-180 , width: self.view.frame.size.width, height: 200))
self.view.addSubview(datePicker)
datePicker.datePickerMode = UIDatePickerMode.dateAndTime
datePicker.isHidden = true
button.isSelected = false
datePicker.addTarget(self, action: #selector(self.dateSelected), for: UIControlEvents.valueChanged)
@objc func dateSelected()
{
let dateformatter1 = DateFormatter()
dateformatter1.dateFormat = "ccc, d MMM yyy"
let dateString1 = dateformatter1.string(from: datePicker.date)
print("Date Selected \(dateString1)")
labelDate.text = dateString1
let dateformatter2 = DateFormatter()
dateformatter2.dateFormat = "dd-MM-yyyy"
let dateString2 = dateformatter2.string(from: datePicker.date)
print("Date Selected \(dateString2)")
let dateformatter3 = DateFormatter()
dateformatter3.dateFormat = "dd/MM/yyyy"
let dateString3 = dateformatter3.string(from: datePicker.date)
print("Date Selected \(dateString3)")
let dateformatter4 = DateFormatter()
dateformatter4.dateFormat = "dd MMMM yyyy hh:mm a"
let dateString4 = dateformatter4.string(from: datePicker.date)
print("Date Selected \(dateString4)")
}
DatePicker and NSDateFormat上的这篇文章对我帮助很大。