根据使用的文本字段更改一个选取器视图的填充

时间:2017-02-09 23:37:05

标签: ios swift uitextfield uipickerview

我是Swift的新手。为了填充我的应用程序中的一些文本字段,我想使用一个选择器视图,它根据我想要填充的文本字段来更改其填充。我的问题是,函数textFieldShouldBeginEditing似乎不起作用。变量textFieldName不会更改其名称。不幸的是,我在这里找到的所有代码仅适用于Objective-C。我希望你能帮我把这段代码付诸实践。

这是我的代码:

@IBOutlet weak var Stadt: UITextField!
@IBOutlet weak var Untergrund: UITextField!
@IBOutlet weak var Groesse: UITextField!
@IBOutlet weak var AnzToreKoerbe: UITextField!
@IBOutlet weak var Platzart: UITextField!
var UntergrundArray = ["Kunstrasen Sand", "Kunstrasen Granulat", "Rasen", "Tartan", "Teer"]
var StadtArray = ["Norderstedt", "Hamburg", "Berlin", "München"]
var GroesseArray = ["1 vs 1", "2 vs 2", "3 vs 3", "4 vs 4", "5 vs 5", "6 vs 6", "7 vs 7"]
var AnzTorKoerbe = ["1", "2", "3", "4"]
var Art = ["Fußball", "Basketball"]
var textFieldName = "Stadt1"

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    if textField.tag == 1{
        textFieldName = "Stadt1"
    }else if textField.tag == 2{
        textFieldName = "Untergrund1"
    }else if textField.tag == 3 {
        textFieldName = "Groesse1"
    }else if textField.tag == 4 {
        textFieldName = "AnzToreKoerbe1"
    }else if textField.tag == 5 {
        textFieldName = "Platzart1"
    }

    return true;
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if (textFieldName.isEqual("Stadt1")) {
        return StadtArray.count
    }else if (textFieldName.isEqual("Untergrund1")) {
        return UntergrundArray.count
    }else if (textFieldName.isEqual("Groesse1")) {
        return GroesseArray.count
    }else if (textFieldName.isEqual("AnzToreKoerbe1")) {
        return AnzTorKoerbe.count
    }else  {
        return Art.count
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if (textFieldName.isEqual("Stadt1")) {
        return StadtArray [row]
    }else if (textFieldName.isEqual("Untergrund1")) {
        return UntergrundArray [row]
    }else if (textFieldName.isEqual("Groesse1")) {
        return GroesseArray [row]
    }else if (textFieldName.isEqual("AnzToreKoerbe1")) {
        return AnzTorKoerbe [row]
    }else  {
       return Art [row]
    }
}

2 个答案:

答案 0 :(得分:1)

您需要在reloadAllComponents方法的末尾调用textFieldShouldBeginEditing

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    if textField.tag == 1{
        textFieldName = "Stadt1"
    }else if textField.tag == 2{
        textFieldName = "Untergrund1"
    }else if textField.tag == 3 {
        textFieldName = "Groesse1"
    }else if textField.tag == 4 {
        textFieldName = "AnzToreKoerbe1"
    }else if textField.tag == 5 {
        textFieldName = "Platzart1"
    }

    somePickerViewProperty.reloadAllComponents()

    return true;
}

somePickerViewProperty替换为UIPickerView商店的名称。

还有一种更简单的编写代码的方法:

var UntergrundArray = ["Kunstrasen Sand", "Kunstrasen Granulat", "Rasen", "Tartan", "Teer"]
var StadtArray = ["Norderstedt", "Hamburg", "Berlin", "München"]
var GroesseArray = ["1 vs 1", "2 vs 2", "3 vs 3", "4 vs 4", "5 vs 5", "6 vs 6", "7 vs 7"]
var AnzTorKoerbe = ["1", "2", "3", "4"]
var Art = ["Fußball", "Basketball"]
var currentData = StadtArray

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    if textField.tag == 1{
        currentData = StadtArray
    }else if textField.tag == 2{
        currentData = UntergrundArray
    }else if textField.tag == 3 {
        currentData = GroesseArray
    }else if textField.tag == 4 {
        currentData = AnzTorKoerbe
    }else if textField.tag == 5 {
        currentData = Art
    }

    somePickerViewProperty.reloadAllComponents()

    return true;
}

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

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

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

答案 1 :(得分:0)

您是否已将UITextField委托设置为self?我无法在您提供的代码中看到它,如果未设置委托,则无法调用textFieldShouldBeginEditing等方法。