动态 UIPickerView 菜单

Dynamic UIPickerView menu(s)

我正在尝试创建一个使用 2 个文本字段和 1 个选择器视图的应用。一旦用户点击第一个文本字段,选择器就会弹出。用户从菜单中选择并单击第二个文本字段后,选择器再次弹出,但菜单取决于第一个文本字段中的文本(先前的选择)。

我遇到的问题是当点击第二个字段时选择器出现(在做出第一个选择并显示在第一个文本弓中),菜单不显示第二个阶段的选择.

    import UIKit

    class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet var textField1: UITextField!
@IBOutlet var textField2: UITextField!

var activeTextField = 0

@IBOutlet var picker: UIPickerView!

var characters = ["Letter", "Number"]
var letters = ["A", "B", "C", "D"]
var numbers = ["1", "2", "3", "4", "5"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    picker.hidden = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func textFieldDidBeginEditing(textField: UITextField) {

    textField.resignFirstResponder()

    if (textField == textField1) {

        activeTextField = 0

    }
    else if (textField == textField2) {

        activeTextField = 1

    }

    picker.hidden = false

}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

    return 1

}


func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if (activeTextField == 0) {
        return characters.count

    }
    else if (textField1.text == "Letter") {
            return letters.count
    }
    else if (textField1.text == "Number") {
        return numbers.count

    }


    return component
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    picker.hidden = true
    if (activeTextField == 0) {
        textField1.text = characters[row]
        picker.reloadAllComponents()
    }
    else if (textField1.text == "Letter") {
            textField2.text = letters[row]
    }
    else if (textField1.text == "Number") {
            textField2.text = numbers[row]


    }

}


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

    if (activeTextField == 0) {
        return characters[row]

    }
    else if (textField1.text == "Letter") {
            return letters[row]
        }
    else if (textField1.text == "Number") {
        return numbers[row]

    }

    return nil
}


deinit {
    self.picker.delegate = nil
    self.picker.dataSource = nil
}
    }

我还想指出,点击几次后,滚动选择器并进行选择,菜单保持不变(仍然显示 "Letter" 和 "Number")但是实际选择的值为 "correct"。请帮助我完成这项工作。提前致谢。

在函数 textFieldDidBeginEditing 中,调用 picker.hidden = false 之前,重新加载 pickerview。