Swift: using UIDatePicker as UITextField inputView not updating
Your handleDatePicker
method is instantiating a new DatePicker
each time it's called. You need to add a parameter to the function to pass in the sender (the DatePicker
created in func 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)
}