Проблема с персонализированной клавиатурой с UIPickerView - PullRequest
0 голосов
/ 25 марта 2019

Я пытаюсь создать персонализированную клавиатуру, чтобы заполнить 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, он показывает клавиатуру вида выбора. Я могу выбрать мое значение в моем средстве выбора, но в текстовое поле ничего не попадает ...

Я не знал, где я совершил ошибку ...

Спасибо за вашу помощь

1 Ответ

1 голос
/ 25 марта 2019

Первый: изменить

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

}

Весьма вероятно, что ни один из них не является правдой, поэтому вы получите пустую строку, отправляемую в текстовое поле.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...