UIDatePicker get .date property on "done" button press - ios

I have a custom view with a UIDatePicker and a done button attached at the top.
When I press the done button, I would like to get the time that is in the UIDatePicker.
I receive an error when I press the button (which I expect since the sender is a UIBarButtonItem) and changing the date time works as expected.
Other solutions show separate selectors but, I would like (if possible) to call the same selector and get the selected as if it were scrolling in the picker view.
I thought of changing the selector method to receive it as an id but, that did not work.
This is what I'm doing currently, I feel like it might be something silly:
UIView *v = [[UIView alloc] init];
// Create the date time picker
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
UIDatePicker *pView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
pView.datePickerMode = UIDatePickerModeTime;
[pView addTarget:self action:#selector(dateTimePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
dateTimePicker = pView;
[v addSubview:dateTimePicker];
// done button
CGRect toolBarFrame = CGRectMake(0, 0, 320, 44);
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(dateTimePickerValueChanged:)];
NSArray *toolBarItems = [NSArray arrayWithObjects:flexibleItem, doneButton, nil];
[toolBar setItems:toolBarItems];
[v addSubview:toolBar];
- (void)dateTimePickerValueChanged:(UIDatePicker *)datePicker {
NSLog(#"time on change: %#", datePicker.date);
Image reference:

Write your method without parameter and make your UIDatePicker as a property or global object for entire class so that you can access it from any where.
Now pass your method to both selectors in this way you will achieve date on done button press.
- (void)dateTimePickerValueChanged {
NSLog(#"time on change: %#", datePicker.date);
}

You're right about what's causing this to crash. I think that separating the selectors is the right path. You could have both selectors calling the same method. That way you don't have any copied code.

The correct way to do this is by creating a NSDate object which is accessible as per your requirement. Now from UIDatePicker, create a method for value changed. Whenever this method gets called set the value of date picker .date property to your NSDate object.
On press of Done button, use the value of NSDate object. It will give you the right value.
#interface ViewController(){
NSDate *globalDate;
}
#end
#implementation ViewController
- (void)dateTimePickerValueChanged {
globalDate = datePicker.date;
}
- (IBAction)donePressed {
NSLog(#"%#",globalDate);
}
#end

TIME
datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeTime;
[yourField 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:UIBarButtonItemStylePlain target:self action:#selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[yourField setInputAccessoryView:toolBar];
-(void)ShowSelectedDate
{
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
dateFormat.dateStyle=NSDateFormatterMediumStyle;
[dateFormat setDateFormat:#"hh:mm"];
NSString *str=[NSString stringWithFormat:#"%#",[dateFormat stringFromDate:datePicker.date]];
//assign text to label
yourField =str;
[yourField resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}

Related

I have two text field, I required to open datepicker and another one Timepicker in IOS

I have two textfield, if I click first one, I need to open Datepicker and when I click second textfield I need to open timepicker. but the code is availble in viewdidload so I dont know how to call it seperately.
I required like when I tap first textbox it should open datapicker, when I tap the second textbox it should open the Timepicker.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
//[self.navigationItem.rightBarButtonItem.customView setHidden:NO];
dateformater = [[NSDateFormatter alloc]init];
datepictxt.delegate = self;
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
datepicker = [[UIDatePicker alloc]init];
if (textField.tag)
{
datepicker.datePickerMode = UIDatePickerModeDate;
[self.datepictxt 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:UIBarButtonItemStyleDone target:self action:#selector(showselectiondate:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:space,donebtn, nil]];
[self.datepictxt setInputAccessoryView:toolbar];
}
else
{
datepicker.datePickerMode = UIDatePickerModeTime;
[self.Timepicker 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:UIBarButtonItemStyleDone target:self action:#selector(showselectiontime:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:space,donebtn, nil]];
[self.Timepicker setInputAccessoryView:toolbar];
}
}
-(void)showselectiontime:(id)response
{
[dateformater setDateFormat:#"hh:mm a"];
self.Timepicker.text= [NSString stringWithFormat:#"%#",[dateformater stringFromDate:datepicker.date]];
[self.Timepicker resignFirstResponder];
}
-(void)showselectiondate:(id)response
{
[dateformater setDateFormat:#"dd/MM/yyyy"];
self.datepictxt.text= [NSString stringWithFormat:#"%#",[dateformater stringFromDate:datepicker.date]];
[self.datepictxt resignFirstResponder];
}
You should use the textField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
for selecting pickers to open for textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;{
if(textField==self.datepictxt){
// show date picker
}
}
Currently i think your if statement:
if (textField.tag)
is always gonna be true so it shows date, or you need to allocate a new datePicker, instead of using same one for both time and date modes.
This can be done in the following way:
Let's say you have two textFields:
#property (nonatomic) UITextField *datePickerTextField;
#property (nonatomic) UITextField *timePickerTextField;
You can add a UIDatePicker as an inputView in the following way:
- (void)setDatePickerAsInputViewForTextField: (UITextField *)textField {
// creating UIDatePicker with UIDatePickerModeDate
UIDatePicker *datepicker = [[UIDatePicker alloc] init];
datepicker.datePickerMode = UIDatePickerModeDate;
// creating toolbar above to dismiss the UIDatePicker after selecting date
UIToolbar *inputAccessoryToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showSelectedDate:)];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[inputAccessoryToolBar setItems:#[doneButton, spaceButton]];
// Adding UIDatePicker object as an inputView to the UITextField so that it appears in place of Keyboard
[textField setInputView:datepicker];
[textField setInputAccessoryView:inputAccessoryToolBar];
}
-(void)showSelectedDate:(id)picker {
[self.view resignFirstResponder]; // to dismiss UITextField
// remaining code to extract date from picker
}
Similarly,
- (void)setTimePickerAsInputViewForTextField: (UITextField *)textField {
UIDatePicker *timepicker = [[UIDatePicker alloc] init];
timepicker.datePickerMode = UIDatePickerModeTime;
UIToolbar *inputAccessoryToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showSelectedTime:)];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[inputAccessoryToolBar setItems:#[doneButton, spaceButton]];
[textField setInputView:datepicker];
[textField setInputAccessoryView:inputAccessoryToolBar];
}
-(void)showSelectedTime:(id)picker {
[self.view resignFirstResponder]; // to dismiss UITextField
// remaining code to extract date from picker
}

dismissing a UIPickerView once the user selected all rows

I have a UIPickerView with 2 components where the user can select a font and a font size.
I want to dismiss the view once the user has selected both.
Can I somehow do this using the didSelectRow:inComponent method?
Yes, if you record the component that is provided to the method then when all of your components have had a selection made you can dismiss.
Note that users may not like it very much and adding a toolbar above the picker with a done button might work better.
I would suggest creating a container view to hold the picker and the toolbar (subviews), then show / hide the container view with whatever animations you like (instead if where you currently show the picker view). The picker and the toolbar don't need to be linked in any way, just laid out nicely in the container.
I hope it will help you
UIActionSheet *action = [[UIActionSheet alloc] init];
action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
UIPickerView *pickerFromage=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 0 , 0)];
UIToolbar *pickerAgeToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerAgeToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerAgeToolbar 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(PickerDoneClick)];
[doneBtn setTintColor:[UIColor blackColor]];
[barItems addObject:doneBtn];
[pickerAgeToolbar setItems:barItems animated:YES];
pickerFromage.delegate=self;
pickerFromage.dataSource=self;
pickerFromage.tag=value;
pickerFromage.showsSelectionIndicator=YES;
[action addSubview:pickerAgeToolbar];
[action addSubview:pickerFromage];
[action showInView:self.view];
[action setBounds:CGRectMake(0,0, 320, 464)];`
-(void)PickerDoneClick{
[action dismissWithClickedButtonIndex:0 animated:YES];
action=nil;
}
You can do it this way:
Take a UIView in xib and then add a UIPickerView and a toolbar with UIButton on it. Give the delegate to the UIPickerView and assign an action to the UIButton. Take an outlet of UIView and in your implementation file(ie. .m file). Hide the UIView and assign the frame to the bottom of the screen. Lets say you have named the view as myPickerView.
myPickerView.frame = CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(myPickerView.frame),
CGRectGetHeight(myPickerView.frame));
myPickerView.hidden = YES;
After doing this you can animate the view from bottom to upwards by setting the frame again in animation block, and can also unhide it.
When you press done, again animate the view and set the frame as above.
Below is the code for animating
- (IBAction)showPicker:(UIButton *)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(0,
100,
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
action for done button:
-(IBAction)doneClicked:(id)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
Hope this helps.

Create PopOver Without Arrows and Centered in UIViewController

I am attempting to display a PopOver in the Center of the ipad in Landscape Orientation. I am able to do it now with the code below but it does not have the Properties that are associated with the "popOverViewController" that is linked up on storyboard. I want it to be a view on storyboard so that I am able to work on and edit it. As well as be able to change the size of it to be either 50% or 75% of the ipads full view and no arrow...
Please, anything will help. *Updated code....can now set size. But still not seeing the "popOverViewController" content.
self.popOverViewController = [[PopOverViewController alloc]init];
self.popOver = [[UIPopoverController alloc]
initWithContentViewController:self.popOverViewController];
CGSize size = CGSizeMake(400.0,300.0);
[self.popOver setPopoverContentSize:size];
CGRect rect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 1, 1);
[self.popOver presentPopoverFromRect:rect inView:self.view permittedArrowDirections:0 animated:YES];
Just copy & Paste the below code
UIViewController *popovercontroller=[[UIViewController alloc] init];
UIView *popoverView=[[UIView alloc] initWithFrame:CGRectMake(312,390, 400, 344)];
popoverView.backgroundColor=[UIColor whiteColor];
popovercontroller.contentSizeForViewInPopover=CGSizeMake(400, 300);
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 400, 0)];
[pickerView setTintColor:[UIColor blackColor]];
[pickerView addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
NSString *bs ; //= [NSString alloc];
// //NSDate *newDate = [NSData alloc];
bs = CurrentSelectedDate;
if (bs.length >= 1) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
// //[dateFormatter setDateStyle:NSDateFormatterLongStyle];
// [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"dd-MMM-yyyy"];
// NSDate *myDate = [dateFormatter dateFromString: txtText.text];
pickerView.date = [dateFormatter dateFromString: CurrentSelectedDate];
}
else
{
pickerView.date = [NSDate date];
}
[popoverView addSubview:pickerView];
// pickerView.date = [dateFormatter dateFromString:txtText.text];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
pickerToolbar.barStyle = UIBarStyleDefault;
pickerToolbar.barTintColor=[UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
[pickerToolbar sizeToFit];
self.navigationController.toolbar.barTintColor = [UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
doneBtn.tintColor=[UIColor whiteColor];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
cancelBtn.tintColor=[UIColor whiteColor];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
popovercontroller.view=popoverView;
pickerViewPopup = [[UIPopoverController alloc] initWithContentViewController:popovercontroller];
[pickerViewPopup presentPopoverFromRect:CGRectMake(312, 212, 400, 344) inView:self.view permittedArrowDirections:0 animated:YES];
The reason you can't see any content is because by explicitly recreating a view controller from scratch that you already have hooked up to an outlet, the compiler assumes you'd like to trash the old value and releases the view controller instance IB created. Or, if this is a separate storyboard or XIB, then the proper initializer would be -initWithNibName:bundle:. Just omit/edit the first line, and assuming the outlets are hooked up correctly, you should see what you've put up in IB.
Alright. So I did this...
popOverViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"popOver"];
I've just been working this one out but wanted to do as much in the storyboard as possible which changes the approach a bit:
1) Select the popover in the storyboard and un-tick Directions Up/Down/Left/Right to get rid of the arrows.
To center it on screen override prepare for segue and do something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIStoryboardPopoverSegue* popover = (UIStoryboardPopoverSegue*)segue;
CGFloat horizontalInset = (self.view.frame.size.width - popover.popoverController.popoverContentSize.width) / (CGFloat)2.0;
CGFloat verticalInset = (self.view.frame.size.height - popover.popoverController.popoverContentSize.height) / (CGFloat)2.0;
popover.popoverController.popoverLayoutMargins =
UIEdgeInsetsMake(
verticalInset,
horizontalInset,
verticalInset,
horizontalInset);
}
In my case I also found that the popover segue's corner radius didn't match my design so I change the background color to clear to sort that out i.e. add this to the above block:
popover.popoverController.backgroundColor = [UIColor clearColor];
2) In the destination controller for the segue find the attribute 'Popover' and tick 'Use Explicit Size' and put the size of you want it to appear in popover mode.
Making the background color clear almost worked for me but it messed up the grayed out background. The the part of the view that was directly behind the arrow was clear and the rest of the background way gray. So I changed the color to whatever the grayed out alpha was.
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:newViewController];
popoverController.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.15];

Add a UIButton to UIDatePicker

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.

iOS Subview (UIDatePicker and UIBarButtonItem) does not respond to user input

I am fairly new to iOS programming so this might be a really simple mistake. Anyways, I have a subview which I want to appear on top of a "normal" view such that a user can input a date with a UIDatePicker and confirm his choice with a UIBarButtonItem (which will also prompt the subview to disappear) in a UIToolbar. The problem is that the control elements (Picker and Button) dont respond to tapping and other gestures despite being visible on top of the "normal" view. I noticed that a button which is part of the "normal" view responds instead, even though, this button lies beneath the subview and is not visible. Here is the code which generates the subview with its elements:
UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
newView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 49)];
toolbar.barStyle = UIBarStyleBlack;
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:nil action:#selector(dismissCustom:)];
toolbar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, 49, 320, 250);
datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[datePicker addTarget:self action:#selector(pickerDateChanged:) forControlEvents:UIControlEventValueChanged];
[newView addSubview:datePicker];
[newView addSubview:toolbar];
[self.view addSubview:newView];
[self.view bringSubviewToFront:newView];
newView.tag = 1;
The methods dismissCustom: and pickerDateChanged: are as follows:
-(void)pickerDateChanged:(UIDatePicker *)sender
{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"Picked the date %#", [dateFormatter stringFromDate:[sender date]]);
dateButton.titleLabel.text = [dateFormatter stringFromDate:sender.date];
}
and
-(void)dismissCustom:(UIBarButtonItem *)sender
{
UIView * newView = [[UIView alloc] viewWithTag:1];
[newView removeFromSuperview];
}
Does anybody have an idea what is missing / wrong? Any help is appreciated!
Cheers
At least for the button, you need to add a touchUpInside event for the action:
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

Resources