Home > Net >  How to set one DatePicker for Multiple TextField.?
How to set one DatePicker for Multiple TextField.?

Time:05-19

I'm Trying to Create a Demo, On Register Page I have 3 TextField, From date and To Date and Date of Birth. I want to use Date Picker and Time Picker for all the textfield. I gave delegate and created a action as well, but it is Not working. How can I do this. Can someone guide me.

here is my code

    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()
        }
  }  
   }

CodePudding user response:

I have Created a demo On This earlier Let me show you.

Add this Extension to your Class.

**Extension For TextField **

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
    }
    }

How To use

create variable

var selectedTextField: UITextField?

In Your View DidLoad method

    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)

**Call @objc methods **

@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()
            }
    }  
}

I Hope it will Help.

  • Related