我在“AvailableDayTextField.text = datePicker”行中遇到此错误
@IBOutlet var AvailableDayTextField: UITextField!
let datePicker = UIDatePicker()
func createDatePicker(){
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done , target: nil , action: nil)
toolbar.setItems([doneButton], animated: false)
AvailableDayTextField.text = datePicker
}
最佳答案
喜欢
在您的ViewDidLoad
集上,类似于
datePicker.date = NSDate()
datePicker.datePickerMode = .Date
datePicker.addTarget(self, action: #selector(self.dateTextField), forControlEvents: .ValueChanged)
AvailableDayTextField.inputView = datePicker
并将函数作为func dateTextField(sender: AnyObject) {
var picker = (AvailableDayTextField.inputView as! UIDatePicker)
picker.maximumDate = NSDate()
var dateFormat = NSDateFormatter()
var eventDate = picker.date
dateFormat.dateFormat = "dd/MM/yyyy"
var dateString = dateFormat.stringFromDate(eventDate)
AvailableDayTextField.text = "\(dateString)"
}
调用