Use a pickerView on a outlet collection textfield (swift3)

My code bellow is a uitextfiled that uses a picker view to display a and b. All I want to do is have the is have the same picker view appear for all of the textfields using outlet collection. Textfield is the single textField and mutlipleTextifeld is the outlet collection the one I want to use. I I just want to replace textField with mutlipleTextifield.

             import UIKit

   class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
   let picker = UIPickerView()
   let country = ["a","b"]
    @IBOutlet var textField: UITextField!
    @IBOutlet var mutlipleTextifeld: [UITextField]!

  override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

picker.delegate = self
picker.dataSource = self

textField.inputView = picker


  }

    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
    }
    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return country.count
      }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return country[row]
           }


 public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textField.text = country[row]

self.view.endEditing(false)
 }}

Hi @timswift

If i understood you correctly you need to iterate through your textFields collection and set the inputView property:

for textFieldObject in mutlipleTextifeld
{
    textFieldObject.inputView = picker
}

Hope this helps.

Nikita

HI @nikita_gaydukov
I put

     for textFieldObject in mutlipleTextifeld
    {
textFieldObject.inputView = picker
    }

in view did load however I am having problems with this is code method below.

    public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    mutlipleTextifeld.text = country[row]
    
    self.view.endEditing(false)
} 

Using multipleTextifeld.text below is giving me a error saying that multipleTextifeld has no member text.

mutlipleTextifeld - is an array, you need to get a particular instance of UITextLabel from it.

@nikita_gaydukov Your right but if I can’t use the outlet collection . text then no results are display. I have attached a picture below. I coded in a single textfield.text and did not use a outlet collection to achieve this result.

This topic was automatically closed after 166 days. New replies are no longer allowed.