When I traverse this UIPickerView from top down, the row is reported correctly for each element. However, traversing the UIPickerView from bottom to top, when "Choice 3" is tapped, the row resets itself to zero where it should actually be 2. I added the label for visual proof -- it flashes "2", then resets itself to zero when traversing the UIPickerView as described. Obviously, not being able to depend on the relationship between display element and actual row being accurate breaks things. No idea why this isn't working...
class ViewController3: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
@IBOutlet weak var textLabel: UILabel!
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 4
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
textLabel.text = String(row)
return pickerValues[row]
}
let pickerValues = ["Choice 1", "Choice 2", "Choice 3","Choice 4"]
If the goal is to change a label every time the user turns the "wheel" of the picker view, implement the delegate method pickerView(_:didSelectRow:inComponent:)
.