Search code examples
ioscocoa-touchevent-handlingdatepicker

UIDatePicker: it is not possible to select default value


I have created UIDatePicker and added target for event UIControlEventValueChanged. Default value of the date picker is set to the current date.

Date picker is shown when I click into text field. Then user can select date, click outside the view controller with the date picker, it disappears and text field is set to the selected date.

Problem is when the user wants to select current(default) date. It is necessary to change value from and back to the default value. It is because the date picker fires UIControlEventValueChanged event. I want also to let the user decide, if he wants to fill the date value (if he just clicks outside the view control with date picker).

How is it possible to let the user select the default value of the date picker without changing the value from and back to the default value?


Solution

  • Is it acceptable for you to just set the date in the text field as soon as the text field is shown? That way the user doesn't need to do anything if the current date is what they want in the text field.

    If the text field needs to start blank for some reason, then you could have the current date show in the text field as soon as the user first taps inside it (i.e. at the same time as you display the UIDatePicker).

    If neither of these are acceptable, have you tried setting a target action for another control event, such as UIControlEventTouchDown / UIControlEventDragInside / UIControlEventEditingDidBegin?