Detect if UIDatePicker is rolling?

oscarm picture oscarm · Feb 20, 2011 · Viewed 15.9k times · Source

I'm using a UIDatePicker in a modal view, to select a date. I found that if the view is closed while the date picker is still rolling, I got a EXC_BAD_ACCESS.

How can I detect of the date picker is rolling when the user tab a button?

I had two ideas:

1) detect if the value has changed with this:

[myDatePicker addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];

But I would need to be able to detect if the value WILL change.

2) check if the date is valid, but when the date picker is rolling, the previous date is returned, and of course is valid.

Any ideas?

Answer

Bogatyr picture Bogatyr · Feb 20, 2011

In order to avoid this crash, you don't need to try to predict when the picker will try to send an action method call to your class, instead, you simply remove the picker's event action in your ViewController's dealloc routine.

EDIT: oops, as was pointed out in a comment, the UIDatePicker doesn't have a delegate. But you can assign it action events. So instead, any actions set pointing to the object that is being dealloc'd should be removed.

- (void dealloc
{
    [myPicker removeTarget:self action:... forControlEvents:...];
    [myPicker release];
    [super dealloc];
}