UiPickerview值显示为问号("?")但是在将其分配给文本框时获取正确的值



//我在向下滚动选取器视图时能够在文本框中看到正确的值。但是选取器视图显示为"?"(问号(。我对需要将选取器视图作为文本框的输入视图的位置感到困惑。但这仍然对我面临的错误没有任何意义.

我使用的是 Xcode 9 和 Swift 3.2。

class BillDetailsViewController: UIViewController ,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{
  //Variables  Declaration
  //Picker view outlet
  @IBOutlet var datePickerView: UIPickerView!
  //Picker view datasource - integer array
  var integerArray = [Int](1899...2500)
  //Textbox where picked value to be shown
  @IBOutlet var txtPaidYear: UITextField!
  //ViewLoad
  override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    datePickerView.isHidden = true
    datePickerView.reloadAllComponents()
    datePickerView.dataSource = self
    datePickerView.delegate = self
    txtPaidYear.inputView = datePickerView
  }
  //picker view delegate functions
  public func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
  }
  public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return integerArray.count
  }
  public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    let pickerValue: String = String(integerArray[row])
    return pickerValue
  }
  public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    txtPaidYear.text = String(integerArray[row])
    self.datePickerView.reloadAllComponents()
  }
}

****你必须像这样更改方法,之后它肯定会起作用。并删除您正在使用的文本字段的委托..**

func pickerView

(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int( -> Int {}

func pickerView

(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int( -> String?{}

func pickerView

(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int( {}

我刚刚删除了类中的所有 TextView delgates,它起作用了.仍然需要弄清楚确切的问题是什么.文本视图委托方法之一似乎导致了问题。我需要弄清楚。无论如何问题得到了修复。我刚刚创建了一个独立的项目并实现了UIPIckerView,它的工作非常完美。因此,我在我的项目中做了同样的事情,删除了所有的文本视图委托方法。非常感谢您的支持。

替换:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int) -> String? {}

跟:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {}

最新更新