I was triyng to create a custom UIPickerView bacause I will need many different custom PickerViews to connect to my one UIViewController.
I create the following code:
let estadosArray = ["AC","AL","AP","AM","BA","CE","ES","GO","MA","MT","MS","MG","PA","PB","PR","PE","PI","RJ","RN","RS","RO","RR","SC","SP","SE","TO","DF"]
class EstadosPickerView: UIViewController {
let myTextField: UITextField
var myPickerView: UIPickerView = UIPickerView()
init(ufExpedicaoTextField: UITextField) {
self.myTextField = ufExpedicaoTextField
myTextField.inputView = myPickerView
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension EstadosPickerView: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return estadosArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return estadosArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
myTextField.text = estadosArray[row]
myTextField.resignFirstResponder()
}
}
When I click on the UITextField on my UIViewController, the UIPickerView appears, but it doesn't show any data. It's empty.
What is wrong with my code that doesn't show any data?
Perhaps you forget about definition dataSource and delegate for the picker
class EstadosPickerView: UIViewController {
....
override func viewDidLoad() {
super.viewDidLoad()
myPickerView.delegate = self
myPickerView.dataSource = self
}
}