Reputation: 183
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
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
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