Adding a button to UIDatePicker

12,185

Solution 1

You should create a UIView to hold the picker and the button you need, then animate that view into the visible screen when you need a date entered, animate it away when the button is tapped. I have used a scheme like this where the button is actually the date label and is tapped to begin or end date editing.

Solution 2

I am Posting the code Please declare the objects that are undeclared as per their type Rest you will get it done.. Hope this helps...

#pragma mark DatePickerView
UIImagePickerController* imagePickerController;
UIDatePicker *theDatePicker;
UIToolbar* pickerToolbar;
UIActionSheet* pickerViewDate;
-(void)DatePickerView
{
    pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"How many?"
                                                 delegate:self
                                        cancelButtonTitle:nil
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:nil];

    theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    theDatePicker.datePickerMode = UIDatePickerModeDate;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];
    [dateFormatter setDateFormat:@"dd MMM yyyy"];
    //[dateFormatter setDateFormat:@"MM/dd/YYYY"];

    //[theDatePicker release];
    [theDatePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];


    pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle=UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];   
    NSMutableArray *barItems = [[NSMutableArray alloc] init];   
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
    [barItems addObject:flexSpace];

    [pickerToolbar setItems:barItems animated:YES];       
    [pickerViewDate addSubview:pickerToolbar];
    [pickerViewDate addSubview:theDatePicker];
    [pickerViewDate  showInView:self.view];
    [pickerViewDate setBounds:CGRectMake(0,0,320, 464)];
}

-(IBAction)dateChanged{

    NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
    [FormatDate setLocale: [[[NSLocale alloc]
                              initWithLocaleIdentifier:@"en_US"] autorelease]];
    [FormatDate setDateFormat:@"MM/dd/YYYY"];   
    SelectedTextField.text = [FormatDate stringFromDate:[theDatePicker date]];
}


-(BOOL)closeDatePicker:(id)sender{   
    [pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
    [pickerToolbar release];
    [pickerViewDate release];
    [SelectedTextField resignFirstResponder];   


    return YES;
}

-(IBAction)DatePickerDoneClick{   
    [self closeDatePicker:self];
    tableview.frame=CGRectMake(0, 44, 320, 416);

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the number of rows
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 30;
    //    return [pickerViewArray count];
}

Do the changes as per requirement. This is 100% running code almost used by me in similar application

hAPPY cODING...

Solution 3

This is a demo project for UIDatePicker with Cancel/Done buttons: https://github.com/lenhhoxung86/CustomDatePicker

Share:
12,185
humblePilgrim
Author by

humblePilgrim

enter link description here

Updated on June 13, 2022

Comments

  • humblePilgrim
    humblePilgrim almost 2 years

    Is it possible to add a 'Done' button to a UIDatePickerView,so that users can tap it to dismiss the view.

  • humblePilgrim
    humblePilgrim over 13 years
    thnx. i was thinking along the same.just wanted to make sure