[英]How do I reset a UIPickerView when it is associated with a TextField's inputView?
I have a UITextField that has its inputView set equal to a UIPickerView 我有一个UITextField,其inputView设置为等于UIPickerView
let thePicker = UIPickerView()
txtCategory.inputView = thePicker
thePicker.delegate = self
I have these functions for the picker 我有这些功能供选择器使用
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return categoryList.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return categoryList[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txtCategory.text = categoryList[row]
}
How would I go about setting to picker back to index of 0 after a button is pressed? 按下按钮后,如何将选择器设置回索引0? I think this code will do the job myPicker.selectRow(0, inComponent: 0, animated: false)
, I just don't know what my picker control is named since I set the textfield's input view equal to the picker. 我认为这段代码可以完成myPicker.selectRow(0, inComponent: 0, animated: false)
,我只是不知道我的选择器控件的名字,因为我将文本字段的输入视图设置为与选择器相等。
Once solution would be to implement the func textFieldDidBeginEditing(_ textField: UITextField)
delegate method. 解决方案是实现func textFieldDidBeginEditing(_ textField: UITextField)
委托方法。 And then see if the text field's inputView
is a picker view. 然后查看文本字段的inputView
是否为选择器视图。 If so, reset the picker view as needed. 如果是这样,请根据需要重置选择器视图。
func textFieldDidBeginEditing(_ textField: UITextField) {
if let picker = textField.inputView as? UIPickerView {
picker.selectRow(0, inComponent: 0, animated: false)
}
}
If you have multiple text fields that use a picker view and you only want to reset the picker for some of the text fields, then add a check to see if textField
is one of the ones you need to handle. 如果您有多个使用选择器视图的文本字段,并且只想重置某些文本字段的选择器,则添加检查以查看textField
是否是您需要处理的文本字段之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.