iOS - Dismiss UIDatePicker presented as inputView

14,654

Solution 1

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.

Solution 2

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.

Share:
14,654
8vius
Author by

8vius

Guess the first thing I'd have to say here is that I LOVE PROGRAMMING. If my computer could generate enough heat to cook on it as well, I could combine 2 of my favorite things. I love facing new challenges, even when they sometimes drive me nuts, seem impossible and make me doubt myself, it just leads me to research more and better myself. Along this same line I'd say I'm a pretty humble guy, never liked to toot my own horn; whenever someone tells me that I'm doing something good, did a great job or I'm a genius (for some reason) I tend to stare them down and tell them "don't lie to me", guess that as well keeps me in a state in which I'm always thriving to learn more and better myself. My third great love (besides programming and cooking), are videogames, both to play and (attempt to) make. I love this so much that I tried to get as many other people as I could into it, that's why I started an elective course on game design and development at my university, and I'm proud to say it's full every single semester. I also enjoy reading comics and graphic novels, fantasy and sci-fi books, good wine and cigars.

Updated on June 04, 2022

Comments

  • 8vius
    8vius about 2 years

    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?