Swift ios从pickerView中获取选定值

时间:2016-08-14 14:13:30

标签: ios swift xcode uipickerview

我想知道获取所选pickerView值的正确方法是什么。

现在我的代码是这样的:

var timeLeft: Int?

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView.tag == 0 {
            TimeLeftBtnLabel.text = TimeLeftPickerData[row]

            timeLeft = row

        } 
    }

或者我应该这样做:

var timeLeft: Int?

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView.tag == 0 {
            TimeLeftBtnLabel.text = TimeLeftPickerData[row]

            timeLeft = TimeLeftPicker.selectedRowInComponent(0)    

        } 
    }

1 个答案:

答案 0 :(得分:0)

我会选择第一个例子,因为它更加一致。

如果将来您决定使用多个组件,您的代码将保持不变。