如何在UITextField中打印我的datePicker值

时间:2017-03-14 16:41:57

标签: ios swift datepicker

这是我的代码,我不希望任何按钮触发标签。我希望每次移动选择器时都要更改它:

    @IBOutlet weak var dateLabel: UILabel!

    @IBOutlet weak var datePicker: UIDatePicker!

    let datePickerView:UIDatePicker = UIDatePicker()

    datePickerView.datePickerMode = UIDatePickerMode.date

    datePickerView.addTarget(self, action: datePickerValueChanged(sender: UIDatePicker), for: UIControlEvents.valueChanged)


func datePickerValueChanged(sender:UIDatePicker) {

    let dateFormatter = DateFormatter()

    dateFormatter.dateStyle = DateFormatter.Style.short

    dateFormatter.timeStyle = DateFormatter.Style.short

    dateLabel.text = dateFormatter.string(from: sender.date)

}

1 个答案:

答案 0 :(得分:2)

替换此

 datePicker.addTarget(self, action: datePickerValueChanged(sender: UIDatePicker), for: UIControlEvents.valueChanged)

使用

 datePicker.addTarget(self, action: #selector(self.datePickerValueChanged), forControlEvents: UIControlEvents.valueChanged)