Search code examples
iosswiftxcodeuipickerview

Swift ios get selected value from pickerView


I wonder what the right way to get the selected pickerView value is.

Right now my code is like this:

var timeLeft: Int?

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

            timeLeft = row

        } 
    }

or should I do it like this:

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)    

        } 
    }

Solution

  • I would go for the first example since Its more consistent.

    If in the future you decide to have multiple components, your code will stay the same.