如何在 Swift 3 的 UIPickerView 中获取所选行的字符串值?
Posted
技术标签:
【中文标题】如何在 Swift 3 的 UIPickerView 中获取所选行的字符串值?【英文标题】:How to get the String value of selected row in a UIPickerView in Swift 3? 【发布时间】:2018-01-11 06:50:07 【问题描述】:func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
switch pickerView
case sexPicker:
return sex.count
case agePicker:
return age.count
case proPicker:
return profession.count
default:
return 0
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
switch pickerView
case sexPicker:
return sex[row]
case agePicker:
return age[row]
case proPicker:
return profession[row]
default:
return ""
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
switch pickerView
case sexPicker:
sexText.text = sexPicker.selectedRow(inComponent: 1) *// This gives only the row value but need string value*
case agePicker:
ageText.text = agePicker.selectedRow(inComponent: 1)
case proPicker:
professionText.text = pickerView(proPicker, titleForRow: 0, forComponent: 1) // Cannot call non-function type uipickerview
default:
break
请帮我获取特定选择器视图的选定行的字符串值。
【问题讨论】:
【参考方案1】:这很简单。请使用它的数据模型。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
switch pickerView
case sexPicker:
sexText.text = sex[row]
case agePicker:
ageText.text = age[row]
case proPicker:
professionText.text = profession[row]
default:
break
【讨论】:
如果对您有用,请考虑将其标记为正确答案。 @Ahsan Ebrahim。完成。以上是关于如何在 Swift 3 的 UIPickerView 中获取所选行的字符串值?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 swift 3.0 中编写此代码,特别是如何在 swift3 中使用宏?