36

How would you do that?

I have a form with a UITextField. When I click in it I would like to display a UIDatePicker instead of the default Keyboard that pops-up by default? Note that there are also other elements on my form.

3
  • Why not use a UIDatePicker instead of UITextField? Commented Jan 23, 2011 at 23:41
  • do you have the code for this? I am facing the same issue as well Commented Apr 5, 2011 at 18:10
  • you should really change the selected answer on this. Commented Mar 5, 2014 at 17:36

5 Answers 5

87

I think this is a more official way to do this:

UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
textField.inputView = datePicker;
6
  • This is not working for me in iOS 5. The text field still presents the normal keyboard when clicked. After setting textField.inputView with a valid pointer and then examining it immediately afterward, it's back to nil. Anybody have an idea why? I'm setting it in viewDidLoad.
    – Oscar
    Commented Oct 7, 2011 at 1:12
  • 2
    check and see if your "datePicker" ivar is not nil
    – NSExplorer
    Commented Dec 2, 2011 at 22:01
  • 3
    this is working perfectly fine, but how to hide the date picker after the date has been selected
    – Noor
    Commented Dec 12, 2013 at 3:27
  • @Noor you would just resign the textfield as first responder. eg. [textField resignFirstResponder];
    – ocross
    Commented May 5, 2014 at 21:33
  • 1
    @shashwat You could add a toolbar with a done button as a inputAccessoryView. Or simply consider the selecting of another field as indication that the user wants to use the selected date.
    – Johan Kool
    Commented Jun 16, 2014 at 15:27
9

in .h

UIDatePicker *datePicker;
NSDate *date;
UIToolbar *keyboardToolbar;

in ViewDidLoad:

if (keyboardToolbar == nil) {
    keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    [keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];

    UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:@"Accept" style:UIBarButtonItemStyleDone target:self action:@selector(closeKeyboard:)];

    [keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, accept, nil]];
}

self.txtDate.inputAccessoryView = keyboardToolbar;

datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

self.txtDate.inputView = datePicker;


- (void)datePickerValueChanged:(id)sender{

date = datePicker.date;

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/YYYY"];


[self.txtDate setText:[df stringFromDate:date]];
}

in storyboard add to txtDate -> EditingDidEnd

- (IBAction)establishHour:(id)sender{

hour = hourPicker.date;

NSDateFormatter *hf = [[NSDateFormatter alloc] init];
[hf setDateFormat:@"HH:MM"];

[self.txtHour setText:[hf stringFromDate:hour]];

}

Sets the keyboard as DatePicker and created a toolbar with a button to accept the selection.

2
  • 2
    To improve the quality of your post, please include how/why this code will solve the problem. Commented Oct 4, 2012 at 10:05
  • 1
    - (void)cerrarTeclado:(id)sender{ [self.txtFecha resignFirstResponder]; } Commented Jan 9, 2013 at 20:50
5

I've converted Johan Kool's answer into swift if anyone would like it. I placed the code in viewDidLoad.

let datePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
textField.inputView = datePicker
1
  • Add one more line for iOS 14 and above: datePicker.preferredDatePickerStyle = .wheels Commented Mar 21, 2022 at 12:54
0
(IBAction)estableceHora:(id)sender{
   Hora = horaPicker.date;
   NSDateFormatter *hf = [[NSDateFormatter alloc] init];
   [hf setDateFormat:@"HH:MM"];
   [self.txtHora setText:[hf stringFromDate:Hora]];
}
-2

Added in .h files :

UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 

Added in .m files :

{

NSDateFormatter *df = [[NSDateFormatter alloc] init];

df.dateStyle = NSDateFormatterMediumStyle;

pTxtDate.text = [NSString stringWithFormat:@"%@",

[df stringFromDate:[NSDate date]]];

NSLog(@"pTxtDate.text %@",pTxtDate.text);

[df release];

[self.view addSubview:pTxtDate]; 

[pTxtDate release];

DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];

DatePicker.datePickerMode = UIDatePickerModeDate;

DatePicker.hidden = NO;

DatePicker.date = [NSDate date];

[DatePicker addTarget:self

  action:@selector(LabelChange:)

forControlEvents:UIControlEventValueChanged];

[self.view addSubview:DatePicker];

    [DatePicker release];

}



- (void)LabelChange:(id)sender {

NSDateFormatter *df = [[NSDateFormatter alloc] init];

df.dateStyle = NSDateFormatterMediumStyle;

pTxtDate.text = [NSString stringWithFormat:@"%@",

[df stringFromDate:DatePicker.date]];

}
1
  • Please use the built-in UIDatePicker instead of trying to roll your own - it will save you a lot of time and pain. Commented Sep 12, 2018 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.