在Swift中获取UIPickerViewControl的选定值

Pro*_*994 22 xcode swift ios8

如何在Swift中获取UIPickerViewControl的选定值?

我试过这样的事情:

labelTest.text = Spinner1.selectedRowInComponent(0).description
Run Code Online (Sandbox Code Playgroud)

但这只返回选定的索引.我需要价值.

谁知道怎么做?

Omk*_*lot 64

您必须将选择器视图委托设置为self并覆盖此功能

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])
    }
Run Code Online (Sandbox Code Playgroud)

要么

您可以根据您的使用情况使用它来提取

var selectedValue = pickerViewContent[pickerView.selectedRowInComponent(0)]
Run Code Online (Sandbox Code Playgroud)

其中pickerViewContent是您的dataSource数组


Mar*_*cas 7

Swift 3:想要选择每行的String值

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        let valueSelected = yourDataSourceArray[row] as String
 }
Run Code Online (Sandbox Code Playgroud)


Jea*_*yle 6

斯威夫特4

let selectedYearPicker = pickerData[yearPicker.selectedRow(inComponent: 
print(selectedYearPicker)
Run Code Online (Sandbox Code Playgroud)

要么

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent  component: Int) {
    let yearValueSelected = pickerData[row] as String
    print(yearValueSelected)
}
Run Code Online (Sandbox Code Playgroud)