Display datepicker when tapping on UISearchController (Searchbar) - ios

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.

Related

iOS - can't get rid of keyboard

I have a text field for a user to enter a string, then another text field that when clicked shows a view that slides up with a date picker inside it.
If the user clicks on the first text field and enters a string using the keyboard and then hits the return button on the keyboard the keyboard goes away. But if they click on the date text field after entering a string in the first text field then the keyboard stays on the screen.
I have tried this:
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UITextField *dateField;
....
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
return(![textField isEqual:self.dateField]);
}
It doesn't work.
I also tried this (this is triggered on begin editing when user clicks on dateField):
- (IBAction)dateTextFieldClicked:(id)sender {
[self.nameField resignFirstResponder];
}
and then this:
- (IBAction)dateTextFieldClicked:(id)sender {
[self.view endEditing:YES];
}
and neither of these works either. Is what I'm trying to do even possible? Should I just give up or is there some way that will work?
Try This ..
- (IBAction)dateTextFieldClicked:(id)sender {
[self.nameField resignFirstResponder];
[self.view endEditing:YES]; }
Try this in your viewDidLoad():
[self.nameTextField addTarget:self action:selector#(nameFieldClicked:) forControlEvents:UIControlEventEditingDidEnd]
And correct the parameter in your dataTextFieldClicked() as
- (IBAction)dateTextFieldClicked:(UITextField *)sender {
[sender resignFirstResponder];
}
I just be an iOS Developer when Apple launched Swift, which means the codes I wrote above maybe have some syntax error, but without logical error.
I hope my answer can help you.
There is a easy way, I had the same problem. Take the dateField as a UIButton, not as UITextField. In the Button action set this as follows:
- (IBAction)Date_set:(id)sender
{
[self.view endEditing:YES];
[self YourDatePickerset];
}
Set the title of the button when DatePicker value changed like this
- (void)pickerChanged:(id)sender
{
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"dd LLL yyyy"]; // Date formater
NSString *date = [dateformate stringFromDate:[myPicker date]]; // Convert date to string
NSLog(#"date :%#",date);
[dob_btn setTitle:date forState:UIControlStateNormal];
}
Can you use this snippet and let me tell the result
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
make sure delegate is set to self for all textfields
Try this also
EDIT
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[dateField resignFirstResponder];
[nameField resignFirstResponder];
}

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

iOS strange keyboard-hiding behavior when adding a subview

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;

Replacing UITextField input with a UIDatePicker

I have a table view controller with (among others) a cell that is to represent a date. I followed this post "How do I make a modal date picker that only covers half the screen?" and I have it working with one big exception - I can't get the picker to disappear!
I tried registering for the event UIControlEventTouchUpOutside, but it seems that this is not generated by the picker (or at least in the mode that I am using it).
How can I recognize that the user has finished selecting a date?
Also, is there a way to disable the user from inputting directly into the UITextField? I want to force them to use the picker. I saw this post "Disable blinking cursor in UITextField?", but is there another way?
Reagards,
--John
Try this code.. here I am putting an datepicker to a uitextfield.. it will have a done button at the top right navigation bar.. so by clicking done I will user can dismiss the datepicker.. the another best method is by putting a toolbar above the datepicker having the done button.. Try this it will work.. when changing the datepicker you can populate the text field.. Hope this helps..
see this stackoverflow link https://stackoverflow.com/a/4824319/763747 this will have the datepicker with done button as toolbar above the keybord..
#pragma mark -
#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return TRUE;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
itsRightBarButton.title = #"Done";
itsRightBarButton.style = UIBarButtonItemStyleDone;
itsRightBarButton.target = self;
itsRightBarButton.action = #selector(doneAction:);
if ([textField isEqual:itsIncidentDateTextField])
{
itsDatePicker = [[[UIDatePicker alloc] init] autorelease];
itsDatePicker.datePickerMode = UIDatePickerModeDate;
[itsDatePicker addTarget:self action:#selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged];
//datePicker.tag = indexPath.row;
textField.inputView = itsDatePicker;
}
}
- (IBAction) incidentDateValueChanged:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]];
[dateFormatter release];
}
I used a different method for disappearing the UIDatePicker.
create a XIB (nib) file and add to it a UIDatePicker then resize the view so it fits only the UIDatePicker, create a property (make it strong and nonatomic) in your ViewController (or whatever class your using, and synthesize of course).
#property (nonatomic, strong) UIView *myDatePickerView;
#synthesize myDatePickerView;
then create a loadDatePickerView method
- (void) loadDatePickerView
{
UINib *nib = [UINib nibWithNibName:kNIBname bundle:[NSBundle mainBundle]];
NSArray *views = [nib instantiateWithOwner:self options:nil];
self.myDatePickerView = [views firstObject];
[myDatePickerView setFrame:CGRectMake(0, 318, 320, 162)];
[self.view addSubview:myDatePickerView];
}
implement the UITextFieldDelegate, and in the textFieldDidBeginEditing method call the loadDatePickerView method,
[self loadDatePickerView];
to make function create a property which is a UIDatePicker instance
#property (nonatomic,strong)
UIDatePicker *myDatePicker;
(synthesize of course)
now create an IBAction like so:
-(IBAction)datePickerValueChanged:(UIDatePicker *)sender
{
myDatePicker = [[myDatePickerView subviews] lastObject];
//now you can do whatever you want with the DatePicker
}
now connect the IBAction to the picker in the XIB file, that way the XIB is now the UIDatePicker instance you created in the VC, if you want it to disappear you can add a UITapGestureRecognizer (in the ViewDidLoad) and the selector will be another IBAction which removes myDatePickerView from its' superView like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dropPicker:)];
[self.view addGestureRecognizer:tap];
[self datePickerValueChanged:myDatePicker];
}
-(IBAction)dropPicker:(id)sender
{
[myDatePickerView removeFromSuperview];
}

Resources