Glatteisen
Glatteisen

Reputation: 173

PickerView - open the last selected row

I have 2 pickerViews. One of them has his own customPickerViewClass and both aren't in the ViewController. I've done it to make them talk with the ViewController. But what I want to do now is, when a row was selected and the title saved in a variable, how can I make it select this row automatically when opening the pickerView again?

Upvotes: 0

Views: 1679

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

So if you save a variable with the selected value you can do the following:

if let index = array.index(where: { $0 == variable }) {
    picker.selectRow(index, inComponent: 0, animated: false)
}

You search for the index in your array source which you use to populate your picker, then you find the the index by searching for the variable which is the one you have saved. Then you select that row in your picker.

Upvotes: 1

Related Questions