Search code examples
swiftuipickerview

The item appearing when I open my UIPickerView is not considered "selected"


When I open my UIPickerView the item appearing is not considered selected in didSelectRow, I have to move up and down and reselect it

This is an extra step for the user if the item appearing is the item he/she actually wants to select

Is there a workaround that I can use to prevent this?

I've tried pickerView.selectRow(selectedRow, inComponent: 0, animated: false) which actually let me display the right item but whatever is displayed is not picked up by pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

Here's how I build my picker:

customPickerView = UIPickerView()
customPickerView.delegate = self
textField.inputView = customPickerView
customPickerView.backgroundColor = UIColor(named: "whiteToBlack")
slwpDatesPickerData = dateArrayRange(inclusiveStartDate: Date(), inclusiveRange: 10)
customPickerView.selectRow(0, inComponent: 0, animated: false)

Solution

  • I was able to force the selection by adding pickerview.delegate?.pickerView?(pickerview, didSelectRow: 0, inComponent: 0) after pickerview.selectRow(selectedRow, inComponent: 0, animated: false)