我刚开始学习iOS开发,这里我想显示一个datePicker并从用户那里获取日期,但我不知道如何从屏幕底部显示一个DataPicker。搜索之后,我发现:textPield可以通过“myTextField.inputView = myDatepicker”激活dataPicker。我做到了,但我遇到了问题:
激活datePicker后,用户可以剪切,粘贴textField中显示的日期。有没有办法禁用textField和编辑菜单中的内容选择?
如果我想使用UIButton / UILabel / cell激活dataPicker而不是使用textField,我该怎么做?(我的意思是如何从底部显示DatePicker。我找到了UIButton / UILabel / cell没有方法.inputView像textField那样)
这可能很简单,但我真的很困惑,这是我想要做的第一个应用程序。任何帮助都非常感谢,特别是在Swift中。非常感谢你。
答案 0 :(得分:0)
方式1
使用按钮显示日期,使用按钮显示/隐藏视图中嵌入的日期选择器。
class ViewController: UIViewController { @IBOutlet weak var dateLabel: UILabel! @IBOutlet weak var datePicker: UIDatePicker! @IBOutlet weak var chooseDate: UIButton! @IBAction func toggleDatePicker(_ sender: Any) { datePicker.isHidden = !datePicker.isHidden } override func viewDidLoad() { dateLabel.text = stringFrom(date: Date()) datePicker.isHidden = true } }
extension ViewController: UIPickerViewDelegate { @IBAction func valueChanged() { dateLabel.text = stringFrom(date: datePicker.date) } private func stringFrom(date: Date) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" return dateFormatter.string(from: datePicker.date) } }
方式2
使用按钮显示日期,并使用按钮从底部显示/隐藏日期选择器。
只需使用我写的以下协议:
protocol DatePickable: class { var textField: UITextField { get } var datePicker: UIDatePicker { get } var isDatePickerVisible: Bool { get set } } extension DatePickable { func prepareDatePickerFor(view: UIView, onChangeAction: Selector) { datePicker.datePickerMode = UIDatePickerMode.date datePicker.addTarget(self, action: onChangeAction, for: .valueChanged) textField.inputView = datePicker view.addSubview(textField) } func toggleDatePicker() { isDatePickerVisible ? hideDatePicker() : showDatePicker() isDatePickerVisible = !isDatePickerVisible } private func showDatePicker() { textField.becomeFirstResponder() } private func hideDatePicker() { textField.resignFirstResponder() } }
如何在课堂上使用该协议:
class ViewController: UIViewController, DatePickable { // Required by Datepickable var isDatePickerVisible = false var textField: UITextField = UITextField() var datePicker: UIDatePicker = UIDatePicker() // IBOutlets @IBOutlet weak var dateLabel: UILabel! @IBOutlet weak var chooseDate: UIButton! override func viewDidLoad() { prepareDatePickerFor(view: view, onChangeAction: #selector(ViewController.onDidChangeDate(_:))) dateLabel.text = stringFrom(date: Date()) } @IBAction func toggleDatePickerVisibility(_ sender: Any) { toggleDatePicker() } @IBAction func onDidChangeDate(_ sender: Any) { dateLabel.text = stringFrom(date: datePicker.date) } private func stringFrom(date: Date) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" return dateFormatter.string(from: datePicker.date) } }
答案 1 :(得分:0)
因此,您必须添加文本字段委托,然后识别正确的文本字段:
let datePicker = UIDatePicker()
func viewDidLoad() {
super.viewDidLoad()
txtDatePicker.delegate = self
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == txtDatePicker {
showDatePicker()
}
return true
}
然后在showDatePicker
内,您将告诉您的选择器应该采用的模式如下:
func showDatePicker() {
//Formate Date
datePicker.datePickerMode = .date
// if you need a toolbar, here is a good place to define it
// add datepicker to textField
txtDatePicker.inputView = datePicker
}
然后在委托功能或工具栏选择中执行以下操作:
func donedatePicker() {
//For date formate
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
txtDatePicker.text = formatter.string(from: datePicker.date)
self.view.endEditing(true)
}