SNos
SNos

Reputation: 3470

Swift 2 - UIPickerView values of two components

I have a UIPickerView with two components, when I change component[0] the data of thecomponent[1]` is reloaded.

Now I am trying to get the values of both components in one string using didSelectRow however, I cannot get the result I want.

public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if component == 0 {

            String0 = arr0[row]
            pickerView.reloadComponent(1)

        }
        if component == 1{

        String1 = arr1[row]

        }

        print("\(String0), \(String1)")

        // result: " , string1.value"
    }

When I select component[0] the result changes but only for String0 What i am trying to achieve is to get "string0.value, string1.value"

Upvotes: 0

Views: 1625

Answers (1)

deoKasuhal
deoKasuhal

Reputation: 2897

You can use method selectedRowInComponent to get the selected row in each component, The selected row would be your correct index to form the string.

Try:

public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if component == 0 {
        pickerView.reloadComponent(1)

    }
    String0 = arr0[pickerView.selectedRowInComponent(0)]
    String1 = arr1[pickerView.selectedRowInComponent(1)]

    print("\(String0), \(String1)")

    // result: " , string1.value"
}

Upvotes: 2

Related Questions