ios UITextField to edit with uidatepicker

ghiboz picture ghiboz · Jan 18, 2011 · Viewed 9.3k times · Source

I have a uitextfield where is written a date.. how can I show on the bottom of the page a uidatepicker instead of a standard keyboard? thanks in advance

Answer

Evan Mulawski picture Evan Mulawski · Jan 19, 2011

Make sure your class implements the UITextFieldDelegate protocol. Then, override the shouldBeginEditing delegate method to return FALSE:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //check if it is the UITextField you don't want the keyboard for
    if (textField != dateTextField)
        return TRUE;

    //present your UIDatePicker
    //see instructions below

    //Return false to prevent the keyboard from appearing.
    return FALSE;
}

I recommend using another UIViewController that contains a UIDatePicker and use presentModalViewController to show it. This conforms to the User-Interface Guidelines.