How can I get the selected value of a UIPickerViewControl in Swift?
I tried something like this:
labelTest.text = Spinner1.selectedRowInComponent(0).description
But this only returns the selected index. I need the value.
Anyone who knows how to do this?
you will have to set the picker view delegate to self and override this function
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
// use the row to get the selected row from the picker view
// using the row extract the value from your datasource (array[row])
}
or
you can use this to extract as per your usage
var selectedValue = pickerViewContent[pickerView.selectedRowInComponent(0)]
where pickerViewContent is your array of dataSource
Swift 3: supose you want the String value of each row selected
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let valueSelected = yourDataSourceArray[row] as String
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With