Malicious UIDatePicker

I used the UIDatePicker a few times already. Mostly in german Apps. I have never had any problems setting the minimum or maximum date properties.
Yesterday we figured out a strange behavior. The App always quit with a “SIGABRT” and the information

-[UIDateTableCell date]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIDateTableCell date]: unrecognized selector sent to instance 

UIDatePickerCrash.png

at following line

self.datePicker.minimumDate = [NSDate date];

And it only crashes if the datePickerMode is UIDatePickerModeDateAndTime

self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;

First try, use the setter Method instead of Dot-notation:

[self.datePicker setMinimumDate:[NSDate date]];

same effect.

As you can’t always rely on the simulator, we tried the code on the device. -> No crash!

First thought: A further Simulator discrepancy.

Tried it on a different Mac in Simulator -> NO CRASH!

So what was the difference between the two systems?

Same XCode Version
Same OS X Version

Different System Language!

Changed the language of the device to Language EN, Region US and voila, the App crashes at the expected line.

After a long google session without any hint, we tried some rearrangement of code and put the setting of the pickers date before the setting of the minimumDate.

self.datePicker.minimumDate = [NSDate date];
self.datePicker.date = [NSDate date]:

to

self.datePicker.date = [NSDate date]:
self.datePicker.minimumDate = [NSDate date];

Guess what! Now the App runs fine even on the EN_US Simulator and Device!

So you will say: “it’s obvious you must set the pickers date before you can set a minimum date”.

Hm good point, but:

  1. the API tells you, that even if you omit setting the date explicitly “the default is the date when the UIDatePicker object is created“, so the date property is never nil.
  2. if the Picker is shown in “german” style the App won’t crash.

If your Picker is created by a nib-File as a IBOutlet, the date can be set in Interface Builder.

A second solution

set the Mode of the Picker in Interface Builder initially to “Date & Time”.
PastedGraphic-2.png

In your code you can change the Mode afterwards via datePickerMode.

self.datePicker.datePickerMode = UIDatePickerModeDate;

But Attention:
Setting the Mode in Interface Builder to “Date & Time” initially will cause the Picker to ignore a minuteInterval programmatically set in code.

So set “Date & Time” in IB -> self.datePicker.minuteInterval = 15; has no effect.

set “Date” -> self.datePicker.minuteInterval = 15; will be recognized (if you also set the Mode to Date & Time programmatically) – but you have to set also date to avoid crashing.

So have fun.

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.