IOS 6: UIDatePicker in UITextField

I am having problems connecting datepicker with textbox. I tried all the solutions from stackoverflow, but they do not work in ios 6. Can you describe how to do this, I want to use a text field and select a date using datepicker.

+4
source share
2 answers

You must use the inputView property of your UITextField to make it a UITextField instead of a keyboard. iOS6 will take care of everything for you (featuring a compiler with animations instead of keyboards, etc.).

Note. You probably want to add some more inputAccessoryView (usually a UIToolBar with some OK button) so that the user can remove the collector (your OK's IBAction will just call [textField resignFirstResponder] to happen, of course), since UIDatePicker doesn't have a button to confirm the entry (whereas the keyboard has its own "Return Key")

+2
source
 #import "TextfieldwithDatepickerViewController.h" UIActionSheet *pickerViewPopup; @implementation TextfieldwithDatepickerViewController - (void)textFieldDidBeginEditing:(UITextField *)aTextField{ [aTextField resignFirstResponder]; pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)]; pickerView.datePickerMode = UIDatePickerModeDate; pickerView.hidden = NO; pickerView.date = [NSDate date]; UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; pickerToolbar.barStyle = UIBarStyleBlackOpaque; [pickerToolbar sizeToFit]; NSMutableArray *barItems = [[NSMutableArray alloc] init]; UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; [barItems addObject:flexSpace]; UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; [barItems addObject:doneBtn]; UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)]; [barItems addObject:cancelBtn]; [pickerToolbar setItems:barItems animated:YES]; [pickerViewPopup addSubview:pickerToolbar]; [pickerViewPopup addSubview:pickerView]; [pickerViewPopup showInView:self.view]; [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)]; } -(void)doneButtonPressed:(id)sender{ //Do something here here with the value selected using [pickerView date] to get that value [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; } -(void)cancelButtonPressed:(id)sender{ [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; } 
-2
source

All Articles