带有 UIPickerView 的个性化键盘问题

Problem with personalised Keyboard with UIPickerView

我正在尝试创建一个个性化键盘,以从 UIPickerView 键盘的值填充 UITextField

我已经创建了键盘,但是当我 select 在我的键盘中输入一个值时 UITextField 中没有任何内容出现...

首先我声明一个懒惰的UIPickerView

lazy var DurationPicker: UIPickerView = {
        let DurationPicker = UIPickerView()
        DurationPicker.delegate = self
        DurationPicker.dataSource = self

        var flightTime: String = ""
        var row0: String = "0"
        var row1: String = "0"
        var row2: String = "0"
        var row4: String = "0"
        var row5: String = "0"

            row0 = myPickerRow1[DurationPicker.selectedRow(inComponent: 0)]

            row1 = myPickerRow2[DurationPicker.selectedRow(inComponent: 1)]

            row2 = myPickerRow3[DurationPicker.selectedRow(inComponent: 2)]

            row4 = myPickerRow5[DurationPicker.selectedRow(inComponent: 4)]

            row5 = myPickerRow6[DurationPicker.selectedRow(inComponent: 5)]

        print("\(row0)\(row1)\(row2):\(row4)\(row5)")

        if row0 != "0"{
            flightTime = "\(row0)\(row1)\(row2):\(row4)\(row5)"
        }else if row0 == "0" && row1 != "0"  {
            flightTime = "\(row1)\(row2):\(row4)\(row5)"
        }else if row1 == "0" && row2 != "0"  {
            flightTime = "\(row2):\(row4)\(row5)"
        }
        print(flightTime)
        return DurationPicker
    }()

其次,我调用 viewDidLoad 中的右键盘连接到右 UITextfield

flightTimeSe.inputView = DurationPicker

最后我宣布 UIPickerView

let myPickerRow1 = [String](arrayLiteral: "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
    let myPickerRow2 = [String](arrayLiteral: "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
    let myPickerRow3 = [String](arrayLiteral: "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
    let myPickerRow4 = [String](arrayLiteral: ":")
    let myPickerRow5 = [String](arrayLiteral: "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
    let myPickerRow6 = [String](arrayLiteral: "0", "5")

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return myPickerRow1.count
        }else if component == 1{
            return myPickerRow2.count
        }else if component == 2{
            return myPickerRow3.count
        }else if component == 3{
            return myPickerRow4.count
        }else if component == 4{
            return myPickerRow5.count
        }
        return myPickerRow6.count
    }

    func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return myPickerRow1[row]
        }else if component == 1{
            return myPickerRow2[row]
        }else if component == 2{
            return myPickerRow3[row]
        }else if component == 3{
            return myPickerRow4[row]
        }else if component == 4{
            return myPickerRow5[row]
        }
        return myPickerRow6[row]
    }

    func pickerView( pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        var flightTime: String = ""
        var row0: String = "0"
        var row1: String = "0"
        var row2: String = "0"
        var row4: String = "0"
        var row5: String = "0"

        if component == 0 {
            row0 = myPickerRow1[DurationPicker.selectedRow(inComponent: 0)]
        }else if component == 1{
            row1 = myPickerRow2[DurationPicker.selectedRow(inComponent: 1)]
        }else if component == 2{
            row2 = myPickerRow3[DurationPicker.selectedRow(inComponent: 2)]
        }else if component == 4{
            row4 = myPickerRow5[DurationPicker.selectedRow(inComponent: 4)]
        }else if component == 5{
            row5 = myPickerRow6[DurationPicker.selectedRow(inComponent: 5)]
        }
        print("\(row0)\(row1)\(row2):\(row4)\(row5)")
        if myPickerRow1[DurationPicker.selectedRow(inComponent: 0)] != "0"{
            flightTime = "\(myPickerRow1[DurationPicker.selectedRow(inComponent: 0)])\(myPickerRow2[DurationPicker.selectedRow(inComponent: 1)])\(myPickerRow3[DurationPicker.selectedRow(inComponent: 2)]):\(myPickerRow5[DurationPicker.selectedRow(inComponent: 4)])\(myPickerRow6[DurationPicker.selectedRow(inComponent: 5)])"
        }else if myPickerRow1[DurationPicker.selectedRow(inComponent: 0)] == "0" && myPickerRow2[DurationPicker.selectedRow(inComponent: 1)] != "0"  {
            flightTime = "\(myPickerRow2[DurationPicker.selectedRow(inComponent: 1)])\(myPickerRow3[DurationPicker.selectedRow(inComponent: 2)]):\(myPickerRow5[DurationPicker.selectedRow(inComponent: 4)])\(myPickerRow6[DurationPicker.selectedRow(inComponent: 5)])"
        }else if myPickerRow2[DurationPicker.selectedRow(inComponent: 1)] == "0" && myPickerRow3[DurationPicker.selectedRow(inComponent: 2)] != "0"  {
            flightTime = "\(myPickerRow3[DurationPicker.selectedRow(inComponent: 2)]):\(myPickerRow5[DurationPicker.selectedRow(inComponent: 4)])\(myPickerRow6[DurationPicker.selectedRow(inComponent: 5)])"
        }
        flightTimeSe.text = "\(flightTime)"
        print(flightTime)
    }

目前,当我 select 右侧的 TextField 显示选择器视图键盘时。我可以 select 我的选择器中的值,但没有任何内容进入我的文本字段...

我不知道哪里出错了...

感谢您的帮助

第一:改变

func pickerView( pickerView: UIPickerView, didSelectRow row: Int

func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int

其次:你的逻辑很可疑:

if myPickerRow1[DurationPicker.selectedRow(inComponent: 0)] != "0"{

}else if myPickerRow1[DurationPicker.selectedRow(inComponent: 0)] == "0" && myPickerRow2[DurationPicker.selectedRow(inComponent: 1)] != "0"  {

}else if myPickerRow2[DurationPicker.selectedRow(inComponent: 1)] == "0" && myPickerRow3[DurationPicker.selectedRow(inComponent: 2)] != "0"  {

}

其中 none 很可能是正确的,因此您最终将一个空字符串发送到您的文本字段。