7

I have a text field in my UI that when it's selected presents a UIDatePicker instead of the default keyboard, how could I set up a button as to dismiss the picker when the user is done?

8vius
  • 5,786
  • 14
  • 74
  • 136

4 Answers4

9

What I do is have my inputView as a custom view which contains a UIDatePicker and a toolbar above it with a 'Done' button wired up to a method that calls a delegate method to tell the object that owns the UITextField to dismiss the "keyboard" by calling resignFirstResponder.

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • Mind providing some code for setting up the UIBarButtonItem on the UIToolBar? – 8vius Mar 05 '12 at 22:36
  • 1
    @8vius - that's really not the question here. If you really need help with that I suggest reading the docs for `UIBarButtonItem` and `UIToolbar` and if you're still stuck, ask another question. – mattjgalloway Mar 05 '12 at 22:43
  • @mattjgalloway I do this way, but calling `resignFirstResponder` does not bring the custom input view keyboard down – onmyway133 Dec 01 '14 at 06:49
  • 2
    @onmyway133 if you use `textField.inputView = datePicker`, you can then use `view.endEditing(_:)` to dismiss the "keyboard" (which in this case is the UIDatePicker) – vrwim Feb 23 '15 at 11:36
1

Create a UIView (namely customDatePickerView) and in that view ,create datepicker and a done button and a cancel button in the xib. In the textfield delegate:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == textfieldName) {
        [textfieldName resignFirstResponder];
         //show the view
         self.customDatePickerView.hidden = NO;

    }

}//dismisses keyboard

Function on Date Picker Value Changed

- (IBAction)onDatePickerClick:(id)sender {

    NSDateFormatter  *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/YYYY"];
    NSDate *selectedDate = [self.wellRecordDatePicker date];
    NSString *recordDate = [dateFormatter stringFromDate:selectedDate];
    self.yourTextfieldName.text = recordDate;


}

- (IBAction)onDoneBtnClick:(id)sender
{
       self.customDatePickerView.hidden = YES;
}
- (IBAction)onCancelBtnClick:(id)sender
{
       self.customDatePickerView.hidden = YES;
}

Hope this will help you.

Aswathy Bose
  • 4,279
  • 4
  • 32
  • 44
0

You can solve this by adding this

[self.view endEditing:YES];

Add this where you want to dismiss date picker like i have added this on tap gesture
On viewDidLoad added a Tapgesture

 UITapGestureRecognizer *gesRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; // Declare the Gesture.
    gesRecognizer.delegate = self;
    [self.view addGestureRecognizer:gesRecognizer];

After adding this on viewdidLoad Add this method to detect gesture recognizer

- (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer{
    NSLog(@"Tapped");
    [self.view endEditing:YES];
}
Mehedi Hasan
  • 359
  • 3
  • 10
0

I've done a similar thing where I've created a view with a button and the UIDatePicker, then presented that view instead of the UIDatePicker alone. Then the button is just connected to an IBAction that moves the view out.

This way, the button moves in with the UIDatePicker view and they look "connected" to the user. You can even define this UIView in your nib and use addSubview: at runtime.

Ash Furrow
  • 12,391
  • 3
  • 57
  • 92