Search code examples
iphoneuipickerview

UIPickerView crash when scrolling ends, but picker view has been freed already


I have a small problem with an UIPickerView. It is part of a class that is displayed as a SemiModalViewController, which can be dismissed (and freed) at any time by pressing an OK button. This class also implements the UIPickerView delegate functions to accept any selection changes etc.

The problem is that the UIPickerView might still be scrolling to an entry while the OK button is pressed - this of course leads to a crash, because the end of the scrolling will try to call the selection changed delegate in the now already freed parent class for the UIPickerView.

Is there any way to check whether the picker view is in a scrolling state and wait for it to end before I dismiss the parent class? I'd rather leave the delegate functions with the mentioned parent class, as this forms a 'dismissable general Picker' UI component, and I'd rather not like to keep all these classes around until the application ends just to make sure the scrolling of the picker eventually ends ...


Solution

  • Ok, I developed a solution ...

    In fact, all I want to do is make sure the Picker does not try to send a Selection Changed event to a non-existing parent class. In case of the picker still scrolling, that can also mean just discarding any end value it might have reached.

    Unfortunately the UIDatePicker and the UIPickerView also are quite different in behavior, so the solution was a bit more work than I thought.

    For those who are interested:

    I made a singleton class that keeps track of all Pickers I create. This singleton class also serves as delegate for all Picker callbacks - so all pickers will always succeed in calling them. Of course, in case the parent class does not exist anymore, the result will be discarded.

    Once I get rid of a Picker, the singleton marks it for deletion after x seconds, and removes it around that time from the list of Pickers to follow. In fact, I could remove the Picker right away, as the singleton will still take any callback from that picker anyway and just ignore the result.

    Some effort, but it works like a charm now. No more crashes, and defined behavior in all cases :-).