Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Harry Stromfelt
2,985 PointsI can't dismiss my datePickers! I have looked all over the internet and no method seems to work for me.
Hello!
I am trying to have two textFields that have a datepicker override for the keyboard. This has worked, but now I would like to implement a method that lets me dismiss the datepicker (like a keyboard) once the entry is completed.
At the moment I am trying to use the resignFirstResponder method on the textField, though I do not really understand it and it doesn't work!
Any ideas?
Thanks!
The below code shows me trying to do it for the two textField's, one for a time entry alone and one for a date entry alone:
UIToolbar *datePickerToolBar = [[UIToolbar alloc] init]; self.dateTextField.inputAccessoryView = datePickerToolBar;
UIToolbar *timePickerToolBar = [[UIToolbar alloc] init];
self.timeEntryTextField.inputAccessoryView = timePickerToolBar;
UIBarButtonItem *submitDateChange = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(datePickerValueChanged:)];
datePickerToolBar.items = @[submitDateChange];
UIBarButtonItem *submitTimeChange = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(timePickerValueChanged:)];
timePickerToolBar.items = @[submitTimeChange];
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
self.dateTextField.inputView = datePicker;
UIDatePicker *timePicker = [[UIDatePicker alloc] init];
[timePicker setDatePickerMode:UIDatePickerModeTime];
self.timeEntryTextField.inputView = timePicker;
Harry Stromfelt
2,985 PointsYes, I should have specified that. I figured that it would run whenever the submit buttons are selected. Ideal I'd like to do it with a touch outside the picker gesture but these seems more problematic..
Cody Coats
7,469 PointspickerView.hidden = YES might also need to be set for it to hide.
Is the method time/datePickerValueChanged for sure getting called on button press?
Harry Stromfelt
2,985 PointsI am not actually sure. I will add an NSLog to check. Will write back with the results.
Thank you
Harry Stromfelt
2,985 PointsI just got round to adding in the log statement, and it never triggers. So the button doesn't seem to be being activated, or in fact the method is just not being called. I can't think why.. any ideas?
Cody Coats
7,469 PointsA few things to check.
Ensure that the frame of the container can actually contain the button. If the superView's container is smaller than the subview then the method will never be triggered. Also ensure userInteractionEnabled = true on the buttons as well as views.
Harry Stromfelt
2,985 PointsSo far I have tried the userInteractionEnabled = true edit but that didn't do anything unfortunately.
I believe that the frame container is meant to be able to contain the button as I have seen many references online where people are doing the same thing as me. The only thing I can never find is thorough examples of how to dismiss the keyboard after.
Harry Stromfelt
2,985 PointsI got ittttt!!!!
Turns out [toolBar sizeToFit] does the trick. I guess I was adding a toolbar that basically had no size, so you couldn't interact with it. SWEEEETTTT
What does sizeToFit actually do, do you know?
Cheers!
Cody Coats
7,469 PointsResizes and moves the receiver view so it just encloses its subviews. The reason this solved your problem is that the toolbar although it appears at the right sizes it's actual width and height were 0. Calling size to fit adjust its height and width to contain the subviews. Now that it has a size and width it can register touches within itself.
Cody Coats
7,469 PointsCody Coats
7,469 PointsAre you calling
resignFirstResponderintime/datePickerValueChanged?