[英]UIDatePicker returns current date not selected date
我有一个UIDatePicker
,它按照下面的代码打印选定的日期。 但是,它不打印所选日期,仅显示当前日期。
@objc func setupDiscoverButton() {
let cell = Bundle.main.loadNibNamed("FirstTableViewCell", owner: self, options: nil)?.first as! FirstTableViewCell
// Date
let sessionDate = cell.datePicker.date
print("date: \(sessionDate)")
}
这是我为UIDatePicker
定义的地方。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("FirstTableViewCell", owner: self, options: nil)?.first as! FirstTableViewCell
// Date & time picker
cell.datePicker.minimumDate = Date()
cell.datePicker.timeZone = TimeZone.current
// Discover button
cell.discoverButton.addTarget(self, action: #selector(setupDiscoverButton), for: .touchUpInside)
return cell
}
要获取DatePicker的更改,请收听datePickerChanged
datePicker.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)
所以你的细胞会
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("FirstTableViewCell", owner: self, options: nil)?.first as! FirstTableViewCell
// Date & time picker
cell.datePicker.minimumDate = Date()
cell.datePicker.timeZone = TimeZone.current
cell.datePicker.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)
// Discover button
cell.discoverButton.addTarget(self, action: #selector(setupDiscoverButton), for: .touchUpInside)
return cell
}
@objc func datePickerChanged(picker: UIDatePicker) {
print(picker.date)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.