在iOS swift中将datePicker用于多个文本字段

时间:2018-04-23 06:10:18

标签: ios uitextfield swift4 uidatepicker

我有4个textFields,我必须为fromDate设置fromTimetoDatetoTimeuserServiceBooking。现在,我需要用UIDatePicker填写这些textFields,我无法显示文本而且我不知道为什么;也许我完全错了......

我发布了我尝试过的代码,请有人查看并说明如何操作。

代码:

import UIKit

class ConfirmRequestViewController: UIViewController,UITextFieldDelegate {

  @IBOutlet weak var fromDateTF: UITextField!
  @IBOutlet weak var toDateTF: UITextField!
  @IBOutlet weak var fromTimeTF: UITextField!
  @IBOutlet weak var toTimeTF: UITextField!
  var datePicker = UIDatePicker()
  var dateFormatter = DateFormatter()
  var toolBar = UIToolbar()

 override func viewDidLoad() {
        super.viewDidLoad()
 }

 func textFieldDidBeginEditing(_ textField: UITextField) {

        if textField == fromDateTF {

            datePicker.datePickerMode = .date
            fromDateTF.inputView = datePicker

            fromDateTF.text = dateFormatter.string(from: datePicker.date)

            toolBar.sizeToFit()

            let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped))
            toolBar.setItems([doneButton], animated: true)
            fromDateTF.inputAccessoryView = toolBar
        }
        if textField == toDateTF {

            datePicker.datePickerMode = .date
            toDateTF.inputView = datePicker

            toolBar.sizeToFit()

            let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped))
            toolBar.setItems([doneButton], animated: true)
            toDateTF.inputAccessoryView = toolBar
        }
        if textField == fromTimeTF {

            datePicker.datePickerMode = .time
            fromTimeTF.inputView = datePicker

            toolBar.sizeToFit()

            let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped))
            toolBar.setItems([doneButton], animated: true)
            fromTimeTF.inputAccessoryView = toolBar
        }
        if textField == toTimeTF {

            datePicker.datePickerMode = .time
            toTimeTF.inputView = datePicker

            toolBar.sizeToFit()

            let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped))
            toolBar.setItems([doneButton], animated: true)
            toTimeTF.inputAccessoryView = toolBar
        }
    }

    @objc func doneButtonTapped(textField: UITextField) {

        if textField == fromDateTF {

            dateFormatter.dateStyle = .medium
            dateFormatter.timeStyle = .none
            fromDateTF.text = dateFormatter.string(from: datePicker.date)
        }
        if textField == toDateTF {

            dateFormatter.dateStyle = .medium
            dateFormatter.timeStyle = .none
            toDateTF.text = dateFormatter.string(from: datePicker.date)
        }
        if textField == fromTimeTF {

            dateFormatter.dateStyle = .none
            dateFormatter.timeStyle = .medium
            fromTimeTF.text = dateFormatter.string(from: datePicker.date)
        }
        if textField == toTimeTF {

            dateFormatter.dateStyle = .none
            dateFormatter.timeStyle = .medium
            toTimeTF.text = dateFormatter.string(from: datePicker.date)
        }
        self.view.endEditing(true)
    }
 }

1 个答案:

答案 0 :(得分:1)

试试这个。

override func viewDidLoad() {
    super.viewDidLoad()

    fromDateTF.inputView    = datePicker
    toDateTF.inputView      = datePicker
    fromTimeTF.inputView    = datePicker
    toTimeTF.inputView      = datePicker

    toolBar.sizeToFit()
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped))
    toolBar.setItems([doneButton], animated: true)
    fromDateTF.inputAccessoryView   = toolBar
    toDateTF.inputAccessoryView     = toolBar
    fromTimeTF.inputAccessoryView   = toolBar
    toTimeTF.inputAccessoryView     = toolBar
}


func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == fromDateTF {
            datePicker.datePickerMode = .date
        }
        if textField == toDateTF {
            datePicker.datePickerMode = .date
        }
        if textField == fromTimeTF {
            datePicker.datePickerMode = .time
        }
        if textField == toTimeTF {
            datePicker.datePickerMode = .time
        }
    }

@objc func doneButtonTapped() {
    if fromDateTF.isFirstResponder {
        dateFormatter.dateStyle = .medium
        dateFormatter.timeStyle = .none
        fromDateTF.text = dateFormatter.string(from: datePicker.date)
    }
    if toDateTF.isFirstResponder {
        dateFormatter.dateStyle = .medium
        dateFormatter.timeStyle = .none
        toDateTF.text = dateFormatter.string(from: datePicker.date)
    }
    if fromTimeTF.isFirstResponder {
        dateFormatter.dateStyle = .none
        dateFormatter.timeStyle = .medium
        fromTimeTF.text = dateFormatter.string(from: datePicker.date)
    }
    if toTimeTF.isFirstResponder {
        dateFormatter.dateStyle = .none
        dateFormatter.timeStyle = .medium
        toTimeTF.text = dateFormatter.string(from: datePicker.date)
    }
    self.view.endEditing(true)
}