使用选择器查看选定的行文本

时间:2018-10-27 12:31:46

标签: ios swift text uipickerview

我想简单地通过UIPickerView获取选定行的字符串值,例如:

let myString = getString(from: myPickerView, inComponent: 0)

我试图像这样扩展UIPickerViewDelegate类:

extension UIPickerViewDelegate {

    func getString(from pickerView: UIPickerView, inComponent component: Int) -> String {
        return pickerView(pickerView, titleForRow: pickerView.selectedRow(inComponent: component), forComponent: component)
    }

}

但是我得到一个错误:

  

“无法调用非功能类型'UIPickerView'的值”

1 个答案:

答案 0 :(得分:0)

当我替换时它起作用:

func getString(from pickerView: UIPickerView, inComponent component: Int) -> String {
   return pickerView(pickerView, titleForRow: pickerView.selectedRow(inComponent: component), forComponent: component)
}

作者:

func getString(from picker: UIPickerView, inComponent selectedComponent: Int) -> String? {
    return pickerView!(picker, titleForRow: picker.selectedRow(inComponent: selectedComponent), forComponent: selectedComponent)
}