Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting selected value of a UIPickerViewControl in Swift

Tags:

xcode

swift

ios8

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?

like image 998
Programmer1994 Avatar asked Oct 31 '14 12:10

Programmer1994


2 Answers

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

like image 83
Omkar Guhilot Avatar answered Nov 12 '22 10:11

Omkar Guhilot


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
 }
like image 8
Marcos Reboucas Avatar answered Nov 12 '22 10:11

Marcos Reboucas