嗨,我有一个UIPickerView
,里面有字符串,我想要的是根据字符串选择文本:我尝试用伪语言解释自己,我想要得到的是像这样的东西:
if uipicker.gettext ()! = "mystring" {
uipicker.selectext("mystring)
}
我怎样才能快速得到这个?
最佳答案
您很可能有一个Array
字符串来填充您的选择器,假设您有:
let data = ["string 1", "string 2", "string 3"]
你实现你的UIPickerView
是这样的:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return data[row]
}
然后您可以创建一个方法,为给定的字符串
选择选择器中的行
func selectPicker(withText text: String) {
if let index = data.index(of: text) {
picker.selectRow(index, inComponent: 0, animated: true)
} else {
print("text not found")
}
}
你就完成了:)
关于swift - 根据字符串更改选择 UiPickerView 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52111422/