【问题标题】:How do I reset a UIPickerView when it is associated with a TextField's inputView?当 UIPickerView 与 TextField 的 inputView 关联时,如何重置它?
【发布时间】:2018-11-12 18:23:28
【问题描述】:

我有一个 UITextField,它的 inputView 设置为等于 UIPickerView

let thePicker = UIPickerView()
txtCategory.inputView = thePicker
thePicker.delegate = self

我有这些选择器的功能

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]
}

按下按钮后,如何将选择器设置回索引 0?我认为这段代码可以完成myPicker.selectRow(0, inComponent: 0, animated: false) 的工作,我只是不知道我的选择器控件的名称是什么,因为我将文本字段的输入视图设置为等于选择器。

【问题讨论】:

    标签: ios swift uipickerview


    【解决方案1】:

    曾经的解决方案是实现func textFieldDidBeginEditing(_ textField: UITextField) 委托方法。然后查看文本字段的inputView 是否是选择器视图。如果是这样,请根据需要重置选择器视图。

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if let picker = textField.inputView as? UIPickerView {
            picker.selectRow(0, inComponent: 0, animated: false)
        }
    }
    

    如果您有多个使用选取器视图的文本字段,并且您只想重置某些文本字段的选取器,则添加检查以查看 textField 是否是您需要处理的选项之一。

    【讨论】:

    • 工作就像一个魅力!
    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多