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 mutlipleTextifeld: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
for textFieldObject in mutlipleTextifeld
{
textFieldObject.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) {
mutlipleTextifeld.text = country[row]
self.view.endEditing(false)
}}

First set the
dataSourceandDelegateof thepickerViewtoselfie theViewControllerclass. As I see you haven't. So yourviewDidLoad()would be somewhat like this:And your
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)delegatewould be like this:Output