如何为多个TextField设置一个DatePicker。?

How to set one DatePicker for Multiple TextField.?

我正在尝试创建演示,在注册页面上我有 3 个 TextField,起始日期和截止日期以及出生日期。我想对所有文本字段使用日期选择器和时间选择器。我也给了委托并创建了一个动作,但它不起作用。我怎样才能做到这一点。谁能指导一下。

这是我的代码

    txtCopy.delegate = self
    txtConvert.delegate = self
    txtToCopy.delegate = self
    
    
    
    self.txtCopy.datePicker(target: self,
                               doneAction: #selector(doneAction(_:)),
                               cancelAction: #selector(cancelAction(_:)),
                               datePickerMode: .date)
    self.txtConvert.datePicker(target: self,
                               doneAction: #selector(doneAction(_:)),
                               cancelAction: #selector(cancelAction(_:)),
                               datePickerMode: .date)
    self.txtToCopy.datePicker(target: self,
                               doneAction: #selector(doneAction(_:)),
                               cancelAction: #selector(cancelAction(_:)),
                               datePickerMode: .date)


objc
 func cancelAction(_ textField: UITextField) {
      self.txtConvert.resignFirstResponder()
  }

 @objc
 func doneAction(_ textField: UITextField) {
 if let datePickerView = txtCopy!.inputView as? UIDatePicker {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy"
    let dateString = dateFormatter.string(from: datePickerView.date)
     if self.selectedTextField!.tag == 0 {

        self.txtConvert.text = dateString
        print(datePickerView.date)
        print(dateString)
        self.txtConvert.resignFirstResponder()
    }
        else if self.selectedTextField!.tag == 1 {
            self.txtCopy.text = dateString
            print(datePickerView.date)
            print(dateString)
            self.txtCopy.resignFirstResponder()
        } else if self.selectedTextField!.tag == 2 {
            self.txtToCopy.text = dateString
            print(datePickerView.date)
            print(dateString)
            self.txtToCopy.resignFirstResponder()
        }
  }  
   }

我之前创建了一个演示让我给你看。

将此扩展程序添加到您的 Class。

**文本字段的扩展**

extension UITextField {
   func datePicker<T>(target: T,
                   doneAction: Selector,
                   cancelAction: Selector,
                   datePickerMode: UIDatePicker.Mode = .date) {
    let screenWidth = UIScreen.main.bounds.width
    
    func buttonItem(withSystemItemStyle style: UIBarButtonItem.SystemItem) -> UIBarButtonItem {
        let buttonTarget = style == .flexibleSpace ? nil : target
        let action: Selector? = {
            switch style {
            case .cancel:
                return cancelAction
            case .done:
                return doneAction
            default:
                return nil
            }
        }()
        
        let barButtonItem = UIBarButtonItem(barButtonSystemItem: style,
                                            target: buttonTarget,
                                            action: action)
        
        return barButtonItem
    }
    
    let datePicker = UIDatePicker(frame: CGRect(x: 0,
                                                y: 0,
                                                width: screenWidth,
                                                height: 216))
    datePicker.datePickerMode = datePickerMode
    datePicker.preferredDatePickerStyle = .inline
    self.inputView = datePicker
    
    let toolBar = UIToolbar(frame: CGRect(x: 0,
                                          y: 0,
                                          width: screenWidth,
                                          height: 44))
    toolBar.setItems([buttonItem(withSystemItemStyle: .cancel),
                      buttonItem(withSystemItemStyle: .flexibleSpace),
                      buttonItem(withSystemItemStyle: .done)],
                     animated: true)
    self.inputAccessoryView = toolBar
    }
    }

使用方法

创建变量

var selectedTextField: UITextField?

在您的视图中使用 DidLoad 方法

    txtCopy.delegate = self
    txtConvert.delegate = self
    txtToCopy.delegate = self
    
    
    
    self.txtCopy.datePicker(target: self,
                               doneAction: #selector(doneAction(_:)),
                               cancelAction: #selector(cancelAction(_:)),
                               datePickerMode: .date)
    self.txtConvert.datePicker(target: self,
                               doneAction: #selector(doneAction(_:)),
                               cancelAction: #selector(cancelAction(_:)),
                               datePickerMode: .date)
    self.txtToCopy.datePicker(target: self,
                               doneAction: #selector(doneAction(_:)),
                               cancelAction: #selector(cancelAction(_:)),
                               datePickerMode: .date)

**调用@objc 方法**

@objc
func cancelAction(_ textField: UITextField) {
  
        self.txtConvert.resignFirstResponder()
}

@objc
func doneAction(_ textField: UITextField) {
    if let datePickerView = selectedTextField!.inputView as? UIDatePicker {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd-MM-yyyy"
        let dateString = dateFormatter.string(from: datePickerView.date)
         if self.selectedTextField!.tag == 0 {
  
            self.txtConvert.text = dateString
            print(datePickerView.date)
            print(dateString)
            self.txtConvert.resignFirstResponder()
        }
            else if self.selectedTextField!.tag == 1 {
                self.txtCopy.text = dateString
                print(datePickerView.date)
                print(dateString)
                self.txtCopy.resignFirstResponder()
            } else if self.selectedTextField!.tag == 2 {
                self.txtToCopy.text = dateString
                print(datePickerView.date)
                print(dateString)
                self.txtToCopy.resignFirstResponder()
            }
    }  
}