I have a weird problem but I guess you guys can show me the mistake pretty easy. I am using a picker view for multiple text fields. But it only shows the "last array" for every text field and pushes on top of everything the selected row in the last text field. Therefore it seems that every text field controls only the last text field.
Here is the involved code, hope you guys can help me:
class SignUpViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
//Mark: Properties
@IBOutlet weak var genderPicker: UITextField!
@IBOutlet weak var branchePicker: UITextField!
@IBOutlet weak var countryPicker: UITextField!
// Picker View Arrays
let genders = ["male", "female", "non-binary"]
let branches = ["Advertising", "Architecture", "Design: Product", "Design: Graphic", "Design: Fashion", "Design: Others", "Film/ Video & TV", "IT", "Museums & Galleries", "Music", "Photography", "Performing & Visual Arts", "Radio", "Others"]
let countries = ["Austria", "Australia", "USA", "Germany"]
// Picker View
var pickerViewSignUp = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Pickers
pickerViewSignUp.delegate = self
pickerViewSignUp.dataSource = self
// Delegates
genderPicker.textAlignment = .center
genderPicker.inputView = pickerViewSignUp
genderPicker.placeholder = "Select Gender"
branchePicker.textAlignment = .center
branchePicker.inputView = pickerViewSignUp
branchePicker.placeholder = "Select Branch"
countryPicker.textAlignment = .center
countryPicker.inputView = pickerViewSignUp
countryPicker.placeholder = "Select Country"
}
// Mark: PickerViews
// List of the SignUp used Drop Down Items
// TextField delegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
pickerViewSignUp.reloadAllComponents()
return true
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerViewSignUp == genderPicker {
return genders.count
} else if pickerViewSignUp == branchePicker {
return branches.count
} else {return countries.count}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerViewSignUp == genderPicker {
return genders[row]
} else if pickerViewSignUp == branchePicker {
return branches[row] } else {return countries[row]}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerViewSignUp == genderPicker {
genderPicker.text = genders[row]
} else if pickerViewSignUp == branchePicker {
branchePicker.text = branches[row]
} else {countryPicker.text = countries[row]}
}
}