Swift - 如何从许多UITextField读取数据,这些UITextField由UIPickerView生成数据

时间:2018-04-09 19:42:13

标签: ios swift uitextfield realm uipickerview

当我尝试从UITextFields读取数据时出现问题。

enter image description here

我有3个IBOutlets用于textFields(性别,目标和每天的膳食数量)。所有textFields的数据都将由UIPickerView显示。我为所有textFields准备了3个不同的数据集。当我点击第一个textField(性别)时,我有PickerView with Data(男性和女性),之后当我点击UIPickerView上的第二个textField(目标)数据集时,我没有重新加载,但是当我点击第三个TextField(数字)时用餐)pickerView数据重新加载第二个textField的数据。当我从该pickerView中选择一些东西时,设置了第二个textFieled(在点击第三个textField之后!)。它总是像这样工作。 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

查看UITextView delegate。您可以使用textFieldDidBeginEditing(_ textField: UITextField)来检测是否已选择文本字段(成为第一响应者)。

然后使用textField参数检测已选择的文本字段,并根据该字段加载选择器数据。

答案 1 :(得分:0)

您可以将pickerView设置为textField.inputView

下面的示例是两个文本字段共享一个选择器视图,但您可以通过检查选择器视图委托方法中activeTextField的标识来轻松扩展它。

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var myTextFieldOne: UITextField!
    @IBOutlet weak var myTextFieldTwo: UITextField!

    let pickerDataOne = ["A", "B", "C", "D", "E", "F"]
    let pickerDataTwo = ["1", "2", "3", "4", "5", "6"]

    // Holds a reference to current text field
    var activeTextField = UITextField()

    override func viewDidLoad() {
        super.viewDidLoad()
        let myPicker = UIPickerView()
        myPicker.delegate = self
        myPicker.dataSource = self
        myTextFieldOne.inputView = myPicker
        myTextFieldTwo.inputView = myPicker
    }

    // MARK: UITextField Delegate

    func textFieldDidBeginEditing(textField: UITextField!) {
        // Sets tapped textField to active one
        self.activeTextField = textField
        if let picker = self.activeTextField.inputView as? UIPickerView {
            // This is one way to get a reference to the picker view
            picker.reloadAllComponents()
        }
    }

    // MARK: UIPickerView Delegate

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        // You can have a different data source for each text field
        if activeTextField === myTextFieldOne {
            return pickerDataOne.count
        } else if activeTextField === myTextFieldTwo {
            return pickerDataTwo.count
        }
    }

    func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        // Gets the title for each row in picker
        if activeTextField === myTextFieldOne {
            return pickerDataOne[row]
        } else if activeTextField === myTextFieldTwo {
            return pickerDataTwo[row]
        }
    }

    func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // Populates the text field
        if activeTextField === myTextFieldOne {
           myTextFieldOne.text = pickerDataOne[row]
        } else if activeTextField === myTextFieldTwo {
           myTextFieldTwo.text = pickerDataTwo[row]
        }
    }

}

然后,您可以通过获取myTextFieldOne.text来访问任何文本字段中的文本。