0

At the moment I'm adding methods to date pickers to make a change to a variable when the value is changed. Each picker has its own method.

Is there an easy way to combine the methods so I have a single methods which can check which picker was changed, or am I best of having each picker with its own method.

Currently I do

datePickerEnd.addTarget(self, action: #selector(endPickerChange), for: UIControl.Event.valueChanged)
datePickerStart.addTarget(self, action: #selector(startPickerChange), for: UIControl.Event.valueChanged)
datePickerOrdered.addTarget(self, action: #selector(orderedPickerChange), for: UIControl.Event.valueChanged)
datePickerReceived.addTarget(self, action: #selector(receivedPickerChange), for: UIControl.Event.valueChanged)

@objc func endPickerChange () {
    finishedVisible = true
}

@objc func startPickerChange () {
    startedVisible = true
}

@objc func orderedPickerChange () {
    orderedVisible = true
}

@objc func receivedPickerChange () {
    receivedVisible = true
}
2
  • developer.apple.com/documentation/uikit/uidatepicker Commented Aug 22, 2020 at 14:01
  • change your functions to receive the sender as the first argument, eg. @objc func endPickerChange (sender: UIDatePicker) {, then you can just compare it inside the function to do the right thing if sender == datePickerEnd { Commented Aug 22, 2020 at 14:03

2 Answers 2

1

Define your picker's value change events like this:

datePickerEnd.addTarget(self, action: #selector(pickerDidChange), for: UIControl.Event.valueChanged)
datePickerStart.addTarget(self, action: #selector(pickerDidChange), for: UIControl.Event.valueChanged)
datePickerOrdered.addTarget(self, action: #selector(pickerDidChange), for: UIControl.Event.valueChanged)
datePickerReceived.addTarget(self, action: #selector(pickerDidChange), for: UIControl.Event.valueChanged)

Then define the common function like this:

@objc func pickerDidChange(_ picker: UIDatePicker) {
    switch picker {
        case datePickerEnd: print("datePickerEnd changed")
        case datePickerStart: print("datePickerStart changed")
        case datePickerOrdered: print("datePickerOrdered changed")
        case datePickerReceived: print("datePickerReceived changed")
        default: break
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Pass tag for every date picker you are using. Try the code below -

func showDatePicker(forPickerName: String) {
    // Setup the UIDatePicker
    datePicker = UIDatePicker.init()
    datePicker.backgroundColor = UIColor.white
    datePicker.autoresizingMask = .flexibleWidth
    datePicker.datePickerMode = .date
    switch forPickerName{
    case "StartDate":
        datePicker.tag = 0
    case "EndDate":
        datePicker.tag = 1
    default:
        datePicker.tag = 2
    }

    datePicker.addTarget(self, action: #selector(self.dateChanged(_:)), for: .valueChanged)
    datePicker.frame = CGRect(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
    self.view.addSubview(datePicker)
}

In you dateChanged you will get which picker value changed by using tag.

@objc func dateChanged(_ sender: UIDatePicker?) {
    print(sender?.tag)
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    if let date = sender?.date {
        self.view_DOB.tf_TextInput.text = dateFormatter.string(from: date)
    }
    
    switch sender?.tag {
    case 0:
        print("start date", dateFormatter.string(from: date))
    case 1:
        print("end date", dateFormatter.string(from: date))
    default:
        print("Other", dateFormatter.string(from: date))
    }
}

Call first function where you want date picker with related String -

  @IBAction func btnAction_PickedClicked(_ sender: UIButton) {
        switch sender.tag {
        case 0:
             showDatePicker(forPickerName: "StartDate")
        case 1:
             showDatePicker(forPickerName: "EndDate")
        default:
             showDatePicker(forPickerName: "Other")
     }
 }

Copy and paste this code, It will work. Let me know if any issue. Happy coding :)

3 Comments

As far as I know developers should avoid using the TAG property nowadays.
@SalDev and what is the reason nowadays for not doing so?
I read it that Apple discourages us to use it. I don't know the exact source by now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.