How to add a "Done" button with in the DatePicker Through StoryBoard?

User558 picture User558 · Mar 19, 2016 · Viewed 9.3k times · Source

In my app i want to display UIDatePicker when user click on button.and that date save into UITextFiled.I done this things. My problem is when date picker appears there is no done button,How can add that done button. Upto now i tried.

- (IBAction)pickerAction:(id)sender
{
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.hidden=NO;
datePicker.date=[NSDate date];
[datePicker addTarget:self action:@selector(TextTitle:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"];
selectedDate.text=[df stringFromDate:datePicker.date];
} 

-(void)TextTitle:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"M-d-yyyy"];
selectedDate.text = [NSString stringWithFormat:@"%@",
                      [df stringFromDate:datePicker.date]];
 }

How can i add done button with this code. please help me.

Answer

User558 picture User558 · Mar 30, 2016

Answer is

datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeDate;
[TextField1 setInputView:datePicker];

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];

[TextField1 setInputAccessoryView:toolBar];