Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Dismiss UIDatePicker presented as inputView

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?

like image 441
8vius Avatar asked Mar 05 '12 22:03

8vius


2 Answers

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.

like image 88
mattjgalloway Avatar answered Oct 07 '22 17:10

mattjgalloway


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.

like image 28
Aswathy Bose Avatar answered Oct 07 '22 17:10

Aswathy Bose