Recently I came across an elusive bug in our Sound Wand app involving the 2-component UIPickerView used to choose the musical key/scale combination.
The setup is a bit complex but to give you an idea, some “scales” are “special” (eg. “Morning Sunrise”) in that they have pre-defined musical keys so when a user selects one of these special scales, the “key” component in the picker reverts to a blank entry. Conversely, if the user then changes to a non-special scale, like “Major”, then, if the key is blank, it spins to the first non-blank entry, “C”.
A rare condition occurred that was causing a crash. While the crash itself was for a separate reason, it was predicated on the UIPickerView not registering an update in one of the components. It’s a bit tricky to explain, but bare with me if you are suffering from a similar symptom or wish to save some headache in the future…
The user starts on a “special” scale (in component 2) which has a “blank” musical “key” entry in component 1. The user then slides component 1 (the key) to a non-blank value (say Gb). Normally upon release, the code would animate the picker component back to blank in accordance with the rules. However, if the user, after changing the first component, quickly switched to the second component and changed its value to a non-special scale (which requires a non-blank key entry in component 1), component 1 would no longer revert to “blank” as it no longer needed to. But the UIPickerView callback,
-pickerView:didSelectRow:inComponent:, would also not be called for the first component selection. This means it would still be the “blank” entry from before, despite displaying something different, and in my case, this resulted in a crash inducing inconsistency.
Though the callback is skipped, reading the value from the UIPickerView directly, gives the correct value for the component. Thus the solution was to read and set both values regardless of which segment/component the UIPickerView callback was reporting to have been updated.
Good luck and be aware of this elusive UIPickerView bug when coding your iOS apps!