使用UIPickerView的个性化键盘的问题

时间:2019-03-25 12:43:06

标签: ios swift uipickerview custom-keyboard

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

我已经创建了键盘,但是当我在键盘中选择一个值时,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的{​​{1}}中调用右侧的键盘

UITextfield

最后我声明flightTimeSe.inputView = DurationPicker

UIPickerView

当前,当我选择正确的TextField时,它将显示选择器视图键盘。我可以在选择器中选择my值,但没有任何内容可进入我的文本字段...

我不知道自己在哪里犯错...

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

首先:更改

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"  {

}

很可能所有这些都不是真的,因此最终将空字符串发送到您的文本字段。