I have a uitextfield where is written a date.. how can I show on the bottom of the page a uidatepicker instead of a standard keyboard?
thanks in advance
Make sure your class implements the UITextFieldDelegate protocol. Then, override the shouldBeginEditing delegate method to return FALSE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//check if it is the UITextField you don't want the keyboard for
if (textField != dateTextField)
return TRUE;
//present your UIDatePicker
//see instructions below
//Return false to prevent the keyboard from appearing.
return FALSE;
}
I recommend using another UIViewController that contains a UIDatePicker and use presentModalViewController to show it. This conforms to the User-Interface Guidelines.
UITextField provides a mechanism for this. You're supposed to be able to set another view (like a datepicker) as the inputView. See this post:
iPhone datepicker instead of keyboard?
You can use the following code ;
//first of all, you have to declare the datePicker and then use the following code;
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[dateLabel resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
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(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
and additional actions ;
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US"]];
[FormatDate setDateFormat:#"dd.MM.yyyy"];
dateLabel.text = [FormatDate stringFromDate:[datePicker date]];
}
-(BOOL)closeDatePicker:(id)sender{
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[dateLabel resignFirstResponder];
return YES;
}
-(IBAction)doneButtonClicked{
[self closeDatePicker:self];
}
Related
In my app, when the user clicks the UITextField, the UIDatePicker should pop up. However, it is not popping up.When I run it, it just shows 'select the date!' on an action sheet and nothing else.
This is my code from my ViewController.m;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
datePicker.backgroundColor = [UIColor whiteColor];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleDefault;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
//[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US"]];
[FormatDate setDateFormat:#"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[datePicker date]];
}
-(BOOL)closeDatePicker:(id)sender{
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[pd resignFirstResponder];
return YES;
}
-(IBAction)doneButtonClicked{
[self closeDatePicker:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Please I just need help with knowing what I am doing wrong.
Read the description of UIActionSheet in the Xcode docs. It says:
UIActionSheet is not designed to be subclassed, nor should you add
views to its hierarchy.
That last bit is important. You can't add a picker view to an action sheet because you're not supposed to add any views to an action sheet.
BTW, UIActionSheet is was in iOS 8, and we are about to move to iOS 9, so it will have been deprecated for 2 major releases as of this week. Thus you should not be doing new development with it. I suggest you create your own view controller and present it modally rather than trying to use UIActionSheet.
You forgot to show UIActionSheet at the end of -(void)textFieldDidBeginEditing:(UITextField *)textField:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
datePicker.backgroundColor = [UIColor whiteColor];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleDefault;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
//[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
[pickerViewDate showInView:self.view]; // <---- here, maybe you forgot
}
I am trying to open UIPickerView as inputView to UITextfield.
Here is my code in textFieldDidBeginEditing:
UIPickerView *categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)];
categoryPicker.delegate = self;
categoryPicker.dataSource = self;
[categoryPicker setShowsSelectionIndicator:YES];
txtCategory.inputView = categoryPicker;
UIToolbar *_providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
_providerToolbar.barStyle = UIBarStyleBlackOpaque;
[_providerToolbar 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(dismissActionSheet)];
[barItems addObject:doneBtn];
[_providerToolbar setItems:barItems animated:YES];
txtCategory.inputAccessoryView = _providerToolbar;
It's working fine for first time, but when I am selecting any value from UIPickerView it's hiding and my UITextfield is editable and UIPickerView is not showing at that time. What am I doing wrong?
I'd suggest you to move your code to viewDidLoad:. Because textFieldDidBeginEditing: is called when textField loads the keyboard for it. So It is not the right time to change the inputAccessoryView of the textField.
Use this...
NSDate *now = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM, yyyy"];
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker setMaximumDate:now];
datePicker.datePickerMode = UIDatePickerModeDate;
txtForDate.text = [dateFormat stringFromDate:datePicker.date];
[datePicker addTarget:self action:#selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
[txtForDate setInputView:datePicker];
// txtForDate is UITextField
I have following three UITextfields associated with UIDatePickers.
self.taskDate.inputView=[self returnDatePickerView:01];
self.taskStartTime.inputView=[self returnDatePickerView:02];
self.taskEndtime.inputView=[self returnDatePickerView:03];
This is my DatePicker method
- (UIView *)returnDatePickerView:(int)tag{
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *doneButton3 = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(taskDateDone)];
[toolBar1 setItems:[NSArray arrayWithObjects:doneButton3, nil]];
UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
if(tag==01){
datePicker.datePickerMode=UIDatePickerModeDate;
}else{
datePicker.datePickerMode=UIDatePickerModeTime;
}
//Setting user selected date as the date picker default
[datePicker setDate:taskDate animated:YES];
[datePicker addTarget:self action:#selector(taskDatePicked:)forControlEvents:UIControlEventValueChanged];
[dateView addSubview:datePicker];
[dateView addSubview:toolBar1];
return dateView;
}
Here is my call back taskDatePicked
- (void)taskDatePicked:(id)sender
{
NSDateFormatter *systemtimeFormatter = [[NSDateFormatter alloc] init];
NSLog(#"tag %ld",textFieldTag);
//[systemtimeFormatter setTimeZone:[NSTimeZone systemTimeZone]];
if(textFieldTag==01){
taskDate = datePicker.date;
[systemtimeFormatter setDateStyle:NSDateFormatterShortStyle];
self.taskDate.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskDate]];
}else if(textFieldTag==02){
taskStartTime = datePicker.date;
[systemtimeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.taskStartTime.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskStartTime]];
NSLog(#"taskStartTime picked date %#",taskStartTime.description);
}else if(textFieldTag==03){
taskEndTime = datePicker.date;
[systemtimeFormatter setTimeStyle:NSDateFormatterShortStyle];
self.taskEndtime.text=[[NSString alloc] initWithString:[systemtimeFormatter stringFromDate:taskEndTime]];
}
[datePicker reloadInputViews];
}
But this method is not calling for all UITextfieds.
What i did wrong here. Please suggest
Try the following code for using single date picker as input view for multiple text fields with different date/time formats.
Here input views for the text fields are assigned in the text field delegate.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
Consider UIDatePicker *datePicker; is the common date picker and NSInteger textFieldTag; holds the tag of the current text field (Both added in the interface extention).
Make sure tag value is assigned for the text fields and delegate is connected for each fields.Then add the following in the viewDidLoad method.
datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:#selector(taskDatePicked:) forControlEvents:UIControlEventValueChanged];
Add the text field delegate like :
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
switch (textField.tag)
{
case 1:
datePicker.datePickerMode = UIDatePickerModeDate;
break;
case 2:
datePicker.datePickerMode = UIDatePickerModeTime;
break;
case 3:
datePicker.datePickerMode = UIDatePickerModeTime;
break;
default:
break;
}
textField.inputView = datePicker;
textFieldTag = textField.tag;
return YES;
}
And the event triggered with the date picker changes is as follows:
- (void)taskDatePicked:(id)sender
{
UITextField *textField = (UITextField *)[self.view viewWithTag:textFieldTag];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
dateformatter.dateStyle = NSDateFormatterMediumStyle;
switch (textFieldTag)
{
case 1:
[dateformatter setDateFormat:#"dd-MM-YYYY"];
break;
case 2:
[dateformatter setDateFormat:#"hh:mm a"];
break;
case 3:
[dateformatter setDateFormat:#"hh:mm a"];
break;
default:
break;
}
textField.text = [dateformatter stringFromDate:[datePicker date]];
}
Please set the tag value to textFieldTag
- (UIView *)returnDatePickerView:(int)tag{
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *doneButton3 = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(taskDateDone)];
[toolBar1 setItems:[NSArray arrayWithObjects:doneButton3, nil]];
UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
if(tag==01){
datePicker.datePickerMode=UIDatePickerModeDate;
}else{
datePicker.datePickerMode=UIDatePickerModeTime;
}
//Setting user selected date as the date picker default
[datePicker setDate:taskDate animated:YES];
[datePicker addTarget:self action:#selector(taskDatePicked:)forControlEvents:UIControlEventValueChanged];
textFieldTag = tag;
[dateView addSubview:datePicker];
[dateView addSubview:toolBar1];
return dateView;
}
Hi I have a requirement where I need to show date and time in uiactionsheet on an iPad only app.
My google /sof search lead me to the following resource https://github.com/TimCinel/ActionSheetPicker
ActionSheetDatePicker *actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:#"" datePickerMode:UIDatePickerModeDateAndTime selectedDate:[NSDate date] target:self action:#selector(onDateSelected:element:) origin:sender];
actionSheetPicker.hideCancel = YES;
[actionSheetPicker showActionSheetPicker];
It works great except for the life of me I can't find where I can set the minimum , maximum date and intervals.
Please if any one has used this could help that be great. Thank you
Here i am using Actionsheet + UIDatePicker for iPhone/IPod, BUT for iPAD using POPOVERCONTROLLER + UIDATEPICKER.
- (IBAction)showAction:(id)sender
{
aac = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
self.dtPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(DatePickerCancelClick:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick:)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:NO];
if (IS_IPHONE) {
[aac addSubview:pickerDateToolbar];
[aac addSubview:dtPicker];
[self.view addSubview:aac];
}
else {
UIView *view = [[UIView alloc] init];
[view addSubview:pickerDateToolbar];
[view addSubview:dtPicker];
UIViewController *vc = [[UIViewController alloc] init];
[vc setView:view];
[vc setContentSizeForViewInPopover:CGSizeMake(320, 260)];
popover = [[UIPopoverController alloc] initWithContentViewController:vc];
popover.delegate = self;
[popover presentPopoverFromRect:myButton.bounds inView:myButton
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
//Hope you got it. Thanks
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 300)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
Above code is useful for add UIDatePicker to UIActionSheet. In above code you can display Date and time by using UIDatePicker.
I ended up using this bit of code as per ibiren's suggestion from UIDatePicker in UIPopover thread .
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:#selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverContent release];
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:tempButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
//tempButton.frame where you need you can put that frame
I know this answer came up late. But for anyone who would like to use this library…
The new version of ActionSheetDatePicker has the following properties:
NSDate *minimumDate;
NSInteger minuteInterval;
NSCalendar *calendar;
NSTimeZone *timeZone;
NSLocale *locale;
that allow you to modify the basic options of DatePicker as you want.
I have a UITextField that I set the inputView to a UIDatePicker so that when the UITextField is pressed, instead of a keyboard, a UIDatePicker pops up. I would like to also add a toolbar with a 'Next' button on it so that the user could easily jump to the next field, but I am not sure how to do this as I am already altering a view on the UITextField to get the UIDatePicker. Here is the code used in loading the view:
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:#selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
issue.inputView = self.datePicker;
}
Suggestions for getting the 'Next' button added to a toolbar on top of it?
Create a view to use as the text field's inputAccessoryView. This view should contain your buttons (e.g. Next, Cancel, Previous).
Here's the code :
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:self action:#selector(switchToNextField:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, next, nil]];
}
issue.inputAccessoryView = keyboardToolbar;
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
issue.inputView = datePicker;
And in your switchToNextField method, just make the nextField as the firstResponder using [nextTextField becomeFirstResponder];
You can create a view then put any UI elements inside of it.
#property (nonatomic,strong) UIPopoverController *popoverControllerBirthday;
add above to your .h file and sync it at .m #synthesize popoverControllerBirthday;
also change your #synthesize datePicker; to #synthesize datePicker=_datePicker;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==self.yourtextfield) {
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
_datePicker=[[UIDatePicker alloc]init];//Date picker
_datePicker.frame=CGRectMake(0,0,320, 216);
_datePicker.datePickerMode = UIDatePickerModeDate;
[_datePicker setMinuteInterval:5];
[_datePicker setTag:10];
[popoverView addSubview:_datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(result)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Next" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 220, 320, 45);
UIColor *titleColor = [UIColor whiteColor];
[button setTitleColor:titleColor forState:UIControlStateNormal];
// UIImage *doneButtonImage = [UIImage imageNamed:#"create event button.png"]; choose a button background if you ahve one
[button setBackgroundImage:doneButtonImage forState:UIControlStateNormal];
[popoverView addSubview:button];
popoverContent.view = popoverView;
popoverControllerBirthday = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverControllerBirthday.delegate=self;
CGRect myFrame =textField.frame;
myFrame.origin.x = 200;// your coordinates change it as you wish
myFrame.origin.y = 195;
[popoverControllerBirthday setPopoverContentSize:CGSizeMake(320, 265) animated:NO];
[popoverControllerBirthday presentPopoverFromRect:myFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
else{
return YES;
}
}
//handle your result call next uitextfield in your case
-(void) result
{
//in result method you can get date using datepicker.date
if (self.popoverControllerBirthday)
{
[self.popoverControllerBirthday dismissPopoverAnimated:NO];
self.popoverControllerBirthday = nil;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"M d yyyy"];
NSString *parseDatePickerDAte =[dateFormatter stringFromDate:_datePicker.date]; // your string that is choosen at datepicker
// self.textfield.text = parseDatePickerDAte ; set this to your textfield if you want
//google this dont remember exact code for that, just changing your textfield to desired one
[self.nexttextfield becomesFirstResponder];
}
You can use SMDatePicker instead of UIDatePicker. It will create a custom view with UIToolbar and UIDatePicker. You can easy customise toolbar with necessary buttons. Take a look at example project.