ebsp
ebsp

Reputation: 183

Change to rows in two differents components in a UIPickerView

I have the following code that should change the selected rows in two different components in a UIPickerView, but only the first one changes.

[pickerView selectRow:[pickerView selectedRowInComponent:0] inComponent:1 animated:TRUE];   
[pickerView selectRow:[pickerView selectedRowInComponent:1] inComponent:0 animated:TRUE];

But when i do the following, they both change.

[pickerView selectRow:5 inComponent:0 animated:TRUE];
[pickerView selectRow:3 inComponent:1 animated:TRUE];

What is wrong with the first code, why is it not working?

Upvotes: 0

Views: 596

Answers (2)

Franck
Franck

Reputation: 742

Probably because they refer to each others.

You should record the selected row and then use it:

NSInteger selectedRowInFirst = [pickerView selectedRowInComponent:0];
NSInteger selectedRowInSecond = [pickerView selectedRowInComponent:1];

[pickerView selectRow:selectedRowInFirst inComponent:1 animated:TRUE];   
[pickerView selectRow:selectedRowInSecond inComponent:0 animated:TRUE];

Upvotes: 1

gamozzii
gamozzii

Reputation: 3921

You are overriding the value of the selected row in component 1 in the first statement with the value from component 0. Then in the second statement you are using that same value you just copied to set row selection in component 0 again.

Thus you are setting component 0 back to its own original selected value.

You need to do something like this:

int comp1OriginalRowValue = [pickerView selectedRowInComponent:1];
[pickerView selectRow:[pickerView selectedRowInComponent:0] inComponent:1 animated:TRUE];   
[pickerView selectRow:comp1OriginalRowValue inComponent:0 animated:TRUE];

Upvotes: 0

Related Questions