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

问题描述 投票:0回答:1

我正在尝试创建一个个性化的键盘,从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中调用正确的键盘连接到正确的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)
    }

目前,当我选择正确的TextField时,它会显示选择器视图键盘。我可以在我的选择器中选择我的值,但没有任何内容到我的文本域...

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

谢谢你的帮助

ios swift uipickerview custom-keyboard
1个回答
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"  {

}

很可能没有一个是真的,所以你最终会将一个空字符串发送到你的文本字段。

© www.soinside.com 2019 - 2024. All rights reserved.