选择DataPicker Swift 4的默认值

时间:2019-01-10 18:50:34

标签: ios swift uipickerview

我在使用DataPicker时遇到一些问题:如何在DatePicker中选择所需的值。 示例:已经选择了“ 1 ITEM”,并且需要通过按按钮选择“ 2 ITEM”。需要选择item [index + 1],我该怎么做?

func numberOfItemsInPickerView(_ pickerView: AKPickerView) -> Int {

        switch pickerView {
        case yearPickerView:
            return defaultDataSample.years.count
        case monthPickerView:
            return defaultDataSample.months.count
        case quaterPickerView:
            return defaultDataSample.quaters.count
        default:
            return 0
        }
    }

    func pickerView(_ pickerView: AKPickerView, titleForItem item: Int) -> String {

        switch pickerView {
        case yearPickerView:
            return defaultDataSample.years[item]
        case monthPickerView:
            return defaultDataSample.months[item]
        case quaterPickerView:
            return defaultDataSample.quaters[item]
        default:
            return ""
        }

    }

    func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {

        switch pickerView {
        case yearPickerView:
            finantialDataSample.currentYear = Int(defaultDataSample.years[item])

        case monthPickerView:
            finantialDataSample.period = defaultDataSample.months[item]

        case quaterPickerView:
            finantialDataSample.period = defaultDataSample.quaters[item]

        default:
            break
        }

1 个答案:

答案 0 :(得分:0)

只需设置一个变量即可记住最后选择的行,然后移至下一行。

var currentRow = 0
func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {
    switch pickerView {
         case yearPickerView:
            currentRow = item
         default:
            break
    }
}

func buttonaction() { //Button triggers this
      let nextRow = currentRow + 1
      pickerview.selectRow(nextRow, inComponent: 0, animated: true)
}

让我知道这是否对您不起作用。

Niall