Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPickerView selectedRowInComponent: returns stale data after code adjusts it

Tags:

uipickerview

I have a UIPickerView with multiple components. Some values are grayed out, and my pickerView:didSelectRow:inComponent honors this by shifting the picker component in question to the nearest valid value, much as UIDatePicker moves from "30" to "28" when you select "February". Then it calls a delegate method to announce the adjusted value.

When my adjuster method calls my UIPickerView's selectRow:inComponent:animated:YES, the value on screen is correct, but the values from selectedRowInComponent: are stale (from before the adjustment for gray values). However, if I call selectRow:inComponent:animated:NO, the returned values are correct, but the lack of animation is jarring.

I've tried wrapping the adjustment in a beginAnimations:/commitAnimations block and catching the values in UIView's +animationDidStopSelector, but I still get stale values. Has anyone run into this before?

The problem is easy to duplicate.

[picker selectRow:newValue inComponent:i+offset animated:YES];

retValue = [picker selectedRowInComponent:i+offset];

If you have YES to animated it, then retValue is 0 on mind (you call it stale). Changing to NO, retValue will be the same as newValue.

like image 541
Eric Lloyd Avatar asked Jul 10 '09 05:07

Eric Lloyd


1 Answers

I have not noticed this, but an easy hack might be to perform the operation animated, then once the animation is complete, just perform it again without the animation. Then the results from selectedRowInComponent should be correct.

However, it seems from your test code that it is returning the correct results. If you ask the picker to select the row animated, then the new value isn't set until the animation is complete, so if you are immediately asking for the selected row after just starting the animation (which will not start until the next runloop anyway), then you are going to get the old result.

How exactly are you catching the end of the animation anyway, could you show the code you used?

And another question is, why do you need this to be immediately correct? If you are setting the picker explicitly, then you already know exactly what value is being set, so why do you have to ask it again?

like image 125
Tark Avatar answered Nov 03 '22 13:11

Tark