user7987142
user7987142

Reputation:

Filling a UITextField with a UIPickerView option immediately

My app uses UITextFields which are populated by UIPickerViews, such as the screenshot attached. An example of how these are set up is below.

Currently, when tapping on the UITextField, it brings up the UIPickerView, but the field itself isn't populated until the UIPickerView is scrolled (e.g., to get the UITextField populated with "Yes, surgery" in the below example, the user must first scroll to "Yes, PCI <6h ago", and then back up again).

How do I get this so as soon as the UITextField is tapped the first row of the UIPickerView is used to populate the field?

let prevCardiacIntervPickerView = UIPickerView()  
prevCardiacIntervPickerView.delegate = self  
prevCardiacIntervPickerView.tag = 1  
prevCardiacIntervPickerField.inputView = prevCardiacIntervPickerView  
var prevCardiacIntervPickerOption = ["Yes, surgery", "Yes, PCI <6h ago", "Yes, PCI >6h ago", "No"]  
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}  
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView.tag == 1 {
            return prevCardiacIntervPickerOption.count
        }
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView.tag == 1 {
            return prevCardiacIntervPickerOption[row]
        }
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView.tag == 1 {
            prevCardiacIntervPickerField.text = prevCardiacIntervPickerOption[row]
        }
}

Example screenshot

Upvotes: 0

Views: 1140

Answers (3)

Rizwan
Rizwan

Reputation: 3666

You can modify following function to

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if row.index == 1 && textField.text.count == 0 {
            // set titleForRow to the UITextField
        }
        if pickerView.tag == 1 {
            return prevCardiacIntervPickerOption[row]
        }
}

There is a check that if UITextfield is yet empty and row.index is one then value of titleForRow should get populated in designated UITextfield

Other quick solution could be, fill UITextField with prevCardiacIntervPickerOption[0] in viewDidAppear itself. But in this case UITextfield will come populated with default value of first array element. If you want to keep UITextfieldempty untill its selected once then go for first solution.

Upvotes: 1

Samer Murad
Samer Murad

Reputation: 2756

It's generally not such a good Idea to have a UITextField be used in correlation with a picker, I mean, if the options are only selectable, then why allow free text? you could just used a selectable row to open the picker and have a default value.

But if you need this exact behavior, I would set a delegate on my textfield and check if it's empty on the first press:

self.myTextField.delegate = self

func textFieldDidBeginEditing(_ textField: UITextField) {
  if textField == myTextField && !textField.text {
     textField.text = pickerViewDataSource[0]
  }
}

Upvotes: 0

Mahendra
Mahendra

Reputation: 8904

You need to implement textFieldShouldBeginEditing: method.

Set delegate

yourTextfield.delegate = self

Implement delegate method

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {


     if (textField == yourTextfield) {  

        // your logic goes here...
        yourTextfield.text = prevCardiacIntervPickerOption.first

     }

   return true
}

Upvotes: 0

Related Questions