Is it possible to add a 'Done' button to a UIDatePickerView,so that users can tap it to dismiss the view.
+1
A:
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.
Adam Eberbach
2010-09-02 06:03:01
thnx. i was thinking along the same.just wanted to make sure
Mithun Madhav
2010-09-02 08:09:32
@Adam Thanx i got it working
Mithun Madhav
2010-09-04 04:21:31
A:
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...
Suriya
2010-09-02 06:37:43