iOS strange keyboard-hiding behavior when adding a subview - ios

I have a view with an UITextField and need to show an UIDatePicker (it's hidden below the keyboard) when a button is pressed.
I'm lazily instantiating the date picker because this allows me to show the view way more fast.
This is the code associated with the tap on the button:
- (IBAction)hideKeyboard {
if (!self.datePicker) {
// UIDatePicker allocation and initialization
self.datePicker = ...
...
[self.view addSubview:self.datePicker];
}
[self.textField resignFirstResponder];
}
What happens is that this interfere with the keyboard hiding animation. Actually there is no animation at all.
Another clue I have is that this doesn't happen on the simulator, but only on the actual device (an iPhone 4S).
How can I solve this?
EDIT:
Setting the datePicker as the inputView of the textField doesn't solve my problem.

I can recommend you another solution then hiding keyboard and using button for showing the date picker.
If I understood you correctly you want to show a date picker when user taps on the textfield and then maybe you set the date in textfield.
UITextField has a property
#property (readwrite, retain) UIView *inputView
If you set the inputView property to date picker then the textfield will show date picker instead a keyboard (and even animated like keyboard).
Of course the values need to get your own and set to textfield.
Edit reason: Question edited
I do not know why setting the datepicker as the input view did not help. Anyway if you do not want the keyboard shown and show the datepicker by adding a subview you can use the following delegate method of UITextField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
implement the delegate method. In the method just add pickerview as subview to your view and return NO for not to show the keyboard.
Edit reason: Commend
Is there any reason why you do not use textfield? If not I would use it, because it makes everything easier. If you want to use for some reason the UILabel then you can watch the keyboard notification. When did the keyboard disapeared and then show the datepicker.
For keyboard notifications please refer here, good explained with sample codes, UIKeyboardDidHideNotification should help you
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

Quite simple :
Just add a date picker object as the input value of your textfield. No need to show/hide the keyboard.
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init] ;
[comps setYear:anyInteger];
NSDate *maximumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:anyInteger;
NSDate *minimumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maximumDate];
[datePicker setMinimumDate:minimumDate];
datePicker.datePickerMode = UIDatePickerModeDate;
textField.inputView = datePicker;

Related

Display datepicker when tapping on UISearchController (Searchbar)

I am using UISearchController programmatically, While tapping on Searchbar textField I want to be set Date picker as inputView
#property (strong, nonatomic) UISearchController *searchController;
References for text field : iPhone display date picker on UITextField touch
But unable to able to access Search bar Textfield. Please help.
You can get search textField using following code. Write it in viewDidLoad
UITextField *searchBarTextField = [self.searchController.searchBar valueForKey:#"_searchField"];
[searchBarTextField setInputView:datePicker];
Hope this will help you
First set delegate of UISearchBar then you can use following delegate method :
- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar {
[self showDatePicker];
return NO;
}
-(void) showDatePicker {
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(showSelectedDate:) forControlEvents:UIControlEventValueChanged];
}
- (void)showSelectedDate:(id)sender {
UIDatePicker *picker = (UIDatePicker*)self.date.inputView;
searchBar.text = [self formatDate:picker.date];
}
You can use any date formatter to show date in desired format.

how to customise FSCalendar

I am using FSCalendar https://github.com/WenchaoIOS/FSCalendar I have a button in my VC and FSCalendar in UIView If I click on the button I need to show Current date(some thing like google calendar app) how can I do this ?
You can call the calendar selectDate with today's date in the button action. like below:
NSDate *today = [NSDate date];
[calendar setDate: today];
When you tap the button this will scroll to today with animation and select it.
I hope this helps.
If you want to show the month of today.
calendar.currentPage = [NSDate date];
If you want to 'select' today and show the month of today
[calendar selectDate:[NSDate date]];
Button Action:
- (void)backToToday {
// scroll to page date
self.currentPageDate = self.today;
// default selected date
self.selectedDate = self.today;
self.calendar.currentPage = self.currentPageDate;
[self.calendar selectDate:self.selectedDate scrollToDate:NO];
}

UIPicker cannot be a delegate?

I created an IBOutlet for a UIPicker and named it 'RequestedDate.' I then went on the type
_RequestedDate.delegate = self;
but I keep getting the error:
property 'delegate' not found on object type 'UIDatePicker'
I am relatively new with Xcode and am just starting to work with code. Is there something I am doing wrong or cannot I not have a delegate for a UIPicker?
Thanks~
UIDatePicker uses the target-action pattern instead of the delegate pattern.
You either add a IBAction for the 'value changed' event in Interface Builder or you do it in code like this:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:#selector(datePickerDidChange:) forControlEvents:UIControlEventValueChanged];
// ...
Then you implement the action method, and can retrieve the new date.
- (void)datePickerDidChange:(UIDatePicker *)picker {
NSDate *newDate = picker.date;
NSLog(#"Picker changed date to %#", newDate);
}
UIPickerView and UIDatePicker only look similar, they are completely separate classes. UIPickerView is a subclass of UIView and UIDatePicker is a subclass of UIControl, they are not related.
I don't think UIDatePicker has a delegate property. https://developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html
What are you trying to do exactly?

How do I open UIDatePicker when tapping on UITextField

I have view controller that contains three UITextFields called "Name", "Location" and "Birthday".
I have this code here which opens a UIDatePicker:
// Open date picker
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,245,0,0)];
[self.view addSubview:pv];
return NO;
}
Here I have two problems.
Problem 1
When I tap any field the datepicker opens. I just want to open the datepicker when "Birthday" is tapped.
Problem 2
When I tap on a field, then tap again, it opens another datepicker.
How can I stop this from happening?
Peter
Instead try this on viewDidLoad:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
birtdayTextField.inputView = datePicker; //birtdayTextField is your "birthday text field"
For opening the datepicker only for a specific textField, check the (UITextField *)textField before opening datePicker. You can set tags to each textField and then use that tag to differentiate.
For your second issue, you need to use resignFirstResponder

Custom UITableCell and UIDatePicker

I have created a custom UITableViewCell and assigned it to a custom class.
The custom cell has two labels and one uitextfield in it. Everything works great, but I need to be able to edit the text field with a date picker. I can't seem to figure out how to make the textfield's inputview the date picker? I can do it fine in a regular cell, but can't find a way to access it in my custom cell?
NSDate *eventDt= [gregorian dateByAddingComponents:offsetComponents toDate:[self trialDate] options:0];
// NSLog(#"EVENT DATE: %#", eventDt);
NSDateFormatter* dateF = [[NSDateFormatter alloc] init];
[dateF setDateFormat:#"MMMM dd, yyyy"];
[dateF setTimeZone:[NSTimeZone localTimeZone]];
NSString *eventDtforDisplay = [dateF stringFromDate:eventDt];
resultscell.labelEventDesc.numberOfLines=0;
[resultscell.labelEventDesc sizeToFit];
[resultscell.labelEventDate setText:[NSString stringWithFormat:#"%#",eventDtforDisplay]];
this is my problem----> [resultscell.textEventDate.inputView = [self datePicker];
[resultscell.textEventDate setText:[NSString stringWithFormat:#"%#",eventDtforDisplay]];
return resultscell;
}
In custom cell you can do this very well in the constructor (init method) of the cell as parameter, or create a setter method. so you don't have to touch the UITextField in the parent class, but only handle the inputView in the cells setter method.
Seems also like handling with delegates and protocols could be usefull here. If you don't know how, here a small example: Creating uitableview forms with uitextfields

Resources