Это мое решение ... используйте enum для отслеживания выбранного состояния и добавьте UITextFieldDelegates:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
//Mark: Properties
@IBOutlet weak var genderPicker: UITextField!
@IBOutlet weak var branchePicker: UITextField!
@IBOutlet weak var countryPicker: UITextField!
enum TextFieldType {
case gender
case branche
case country
case none
}
// 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()
var selectedPikerType = TextFieldType.none
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"
genderPicker.delegate = self
branchePicker.textAlignment = .center
branchePicker.inputView = pickerViewSignUp
branchePicker.placeholder = "Select Branch"
branchePicker.delegate = self
countryPicker.textAlignment = .center
countryPicker.inputView = pickerViewSignUp
countryPicker.placeholder = "Select Country"
countryPicker.delegate = self
}
// Mark: PickerViews
// List of the SignUp used Drop Down Items
// TextField delegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
switch textField {
case genderPicker: selectedPikerType = .gender
case branchePicker: selectedPikerType = .branche
case countryPicker: selectedPikerType = .country
default: selectedPikerType = .none
}
pickerViewSignUp.reloadAllComponents()
return true
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch selectedPikerType {
case .branche : return branches.count
case .country : return countries.count
case .gender : return genders.count
case .none : return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch selectedPikerType {
case .branche : return branches[row]
case .country : return countries[row]
case .gender : return genders[row]
case .none : return nil
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch selectedPikerType {
case .branche : branchePicker.text = branches[row]
case .country : countryPicker.text = countries[row]
case .gender : genderPicker.text = genders[row]
case .none : break
}
}
}
Я бы также предложил вам лучшее именование переменных ... Если объект текстовое поле использует префикс "txt" ... так что txtGender и так далее. Это намного понятнее. Используйте также расширения для разделения источников данных и делегатов, что делает все это более читабельным. :)
Вы можете написать расширение следующим образом:
extension ViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
switch textField {
case genderPicker: selectedPikerType = .gender
case branchePicker: selectedPikerType = .branche
case countryPicker: selectedPikerType = .country
default: selectedPikerType = .none
}
pickerViewSignUp.reloadAllComponents()
return true
}
}