本文介绍了从 UIPickerView 接收输入时如何更新 UITextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在以编程方式在 cellForRowAt 中创建一个 UITextFieldUIPickerView.如果我将 UITextField 连接到 IBOutlet,我可以轻松地更新 didSelectRow 中选定的 UIPickerView 值.

Currently, I am programmatically creating a UITextField and UIPickerView within cellForRowAt. I can easily update the selected UIPickerView value within didSelectRow if I connect my UITextField to an IBOutlet.

但是,我需要使用应用的 AutoLayout 约束以编程方式创建 UITextField.

However, I require the UITextField to be created programmatically with applied AutoLayout constraints.

但是,我不知道如何更新 UITextField 因为我在 UIPickerView's didSelectRow 函数中没有引用它,因为它是在 cellForRowAt 中以编程方式创建,如下所示:

However, I don't know how to update the UITextField as I have no reference to it in the UIPickerView's didSelectRow function since it's created programmatically within cellForRowAt like so:

var testing: [String] = []

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    // Dequeue the cell to load data
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    if indexPath.section == 2
    {
        let wearsPickerView: UIPickerView = UIPickerView()

        wearsPickerView.delegate = self
        wearsPickerView.dataSource = self

        let textField: UITextField = UITextField()

        textField.translatesAutoresizingMaskIntoConstraints = false

        textField.textColor = UIColor.black

        textField.text = "Test"

        textField.delegate = self
        textField.inputView = wearsPickerView

        cell.contentView.addSubview(textField)

        let leadingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.leading, multiplier: 1.0, constant: 0)
        let trailingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: 0)
        cell.contentView.addConstraint(leadingConstraint)
        cell.contentView.addConstraint(trailingConstraint)

        let topConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0)
        let bottomConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0)
        cell.contentView.addConstraint(topConstraint)
        cell.contentView.addConstraint(bottomConstraint)
    }

    return cell
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    textField.resignFirstResponder()

    return true
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return testing.count
}

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    // How to update UITextField string?

}

推荐答案

您可以获取对应索引路径的单元格,并找到该单元格中的文本字段.您有 2 个选项可用于查找 textField.一种是使用标签,以防您在该单元格中有多个 UITextField ,而您没有.所以你可以这样做:

You can get the cell for the corresponding index path, and find the textfield in that cell. You have 2 options for finding the textField. One would be to use tags, in case you had more than one UITextFields in that cell, which you don't. So you can do it like this:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
  {
    // choose the correct row. I've assumed you only have one row.
    if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 2)) {
      for subview in cell.subviews {
        if let textField = subview as? UITextField {
          textField.text = testing[row]
          break
        }
      }
    }
  }

这篇关于从 UIPickerView 接收输入时如何更新 UITextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:57