一个ViewController中有2个UIDatepickerView

时间:2018-07-31 07:52:17

标签: ios swift

我想在1 UIDatePickerView中使用2 ViewController。我使用了两个文本字段fromeDatetoDate,但是当我选择fromdate文本字段时,它会自动选择其他toDate

我想知道如何在1个视图控制器中使用2个或更多UIDatePickerview

这是我的代码:

    @IBOutlet weak var fromDateTxtField: UITextField!
    @IBOutlet weak var toDateTxtField: UITextField!

    private var datePickerFrom: UIDatePicker?
    private var datepickerTo : UIDatePicker?

    override func viewDidLoad() {
        super.viewDidLoad()
        datePickerFrom = UIDatePicker()
        datepickerTo = UIDatePicker()
        datePickerFrom?.datePickerMode = .date
        datepickerTo?.datePickerMode = .date
        fromDateTxtField.inputView = datePickerFrom
        toDateTxtField.inputView = datepickerTo
        showDatePicker()

    }

    func showDatePicker(){
        datePickerFrom?.datePickerMode = .date
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneDatePicker))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancle", style: .plain, target: self, action: #selector(cancelDatePicker))

        toolBar.setItems([doneButton, spaceButton, cancelButton], animated: false)
        fromDateTxtField.inputAccessoryView = toolBar
        fromDateTxtField.inputView = datePickerFrom
        toDateTxtField.inputAccessoryView = toolBar
        toDateTxtField.inputView = datepickerTo
    }
    @objc func doneDatePicker ()
    {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/yyyy"
        fromDateTxtField.text = formatter.string(from: (datePickerFrom?.date)!)
        toDateTxtField.text = formatter.string(from: (datepickerTo?.date)!)
        self.view.endEditing(true)
    }

    @objc func cancelDatePicker()
    {
        self.view.endEditing(true)
    }

1 个答案:

答案 0 :(得分:0)

这是两个文本字段

 @IBOutlet weak var txtOnwardDate: UITextField!
 @IBOutlet weak var txtReturnDate: UITextField!  


  var onwardDate: Date?
  var returnDate: Date?

自定义datePicker

func customizeDatePicker() {
        datePicker.datePickerMode = .date
        datePicker.minimumDate = Date()
        datePicker.addTarget(self, action: #selector(datePickerValueChanged), for: .valueChanged)

        let toolBar = UIToolbar()
        let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(donePressed))
        let cancel: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.done, target: self, action: #selector(cancelPressed))

        toolBar.barTintColor = fillColor
        var items: [UIBarButtonItem] = Array()
        items.append(cancel)
        items.append(flexSpace)
        items.append(done)
        toolBar.items = items
        toolBar.sizeToFit()

        txtOnwardDate.inputAccessoryView = toolBar
        txtReturnDate.inputAccessoryView = toolBar
        txtOnwardDate.inputView = datePicker
        txtReturnDate.inputView = datePicker

    }

使用textField委托确定当前选定的textField。

func textFieldDidBeginEditing(_ textField: UITextField) {

            if textField == txtOnwardDate {
                if let onwardDate = onwardDate{
                datePicker.setDate(onwardDate, animated: true)
                }
                datePicker.minimumDate = Date()
            }
            if textField == txtReturnDate {
                if let returnDate = returnDate{
                    datePicker.setDate(returnDate, animated: true)
                }
                datePicker.minimumDate = onwardDate
            }
        }


func datePickerValueChanged()  {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd MMMM, yyyy"
        let dateString = dateFormatter.string(from: datePicker.date)

        if txtOnwardDate.isFirstResponder {
            onwardDate = datePicker.date
            if let onwardDate = onwardDate, let returnDate = returnDate, onwardDate > returnDate {
                txtReturnDate.text = ""
                self.returnDate = nil
            }
            txtOnwardDate.text = dateString
        }else {
            returnDate = datePicker.date
            txtReturnDate.text = dateString
        }
    }