How do I open UIDatePicker when tapping on UITextField - ios

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

Related

UIPickerView isn't showing as inputview when UITextField begins to edit

I have managed to add a UIPickerView as an input view to UITextField in viewDidLoad, but now I want to make the UITextField as first responder when another textfield (with tag =4) ends editing. This is the code I'm using:
This is in the viewdidload, and all the other code needed to initialize the picker view is there:
UIPickerView *myPickerView = [[UIPickerView alloc] init];
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.backgroundColor = [UIColor colorWithRed:76/255.0 green:76/255.0 blue:76/255.0 alpha:1];
textfieldInput.inputView = myPickerView;
This is in my text field did end editing method:
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == 4){
[textfieldInput becomeFirstResponder];
}
The problem is that when the textfieldInput becomes the first responder, the picker view isn't shown, instead the keyboard is presented.
Also, it should be mentioned that i have another textfield on top of this that gets resigned if it became the first responder and becomes hidden. This textfield is the textfield that has a tag of 4. (This textfield has been added for design reasons).
This code works for me... are you sure that call the correct reference of textfield?
try: [self.textfield becomeFirstResponder];

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 always hide keyboard but still can select textField value?

I want my UITextField doesn't show keyboard but the value still can be selected (for example, user wants to move cursor to any index in UITextField)
I have tried to do it this way:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self numberInput].delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[[self numberInput] resignFirstResponder];
}
but the result is, I can't select value from textField. Is there any workaround for this?
You can change the input of the textField.
If you pass an empty UIView, the keyboard will not appear and you should be able to select the text anyway. In swift 2.0:
textField.inputView = UIView()

Adding touchupinside to a programmatically created button, and submitting a NSDate to a textfield

The problem I'm coming across is that I'm unsure how to add touchupinside to the below code for the save date button. I'm just trying to launch a UIDatePicker and submit a date into a text field, and then dismiss it.
So I don't know how to link this programmatically created button to the datePicker method. Not only that but, I haven't used UIDatePicker before, so reading this into a textfield I'm a bit fuzzy on. The text field is dateFieldText.
Once the user taps Save Date then the UIDatePicker should be dismissed.
Thanks for the help. This issue is a bit more complicated for me.
- (void)textFieldDidBeginEditing:(UITextField *)sender
{
sender.delegate = self;
if([sender isEqual:dateFieldText])
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Save Date"
style:UIBarButtonItemStyleDone
target:self
action:#selector(datePicker)];
self.navigationItem.rightBarButtonItem = doneButton;
}
else{
UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self
action:#selector(datePicker)];
self.navigationItem.rightBarButtonItem = submitButton;
}
}
-(IBAction)datePicker
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:#selector(saveDate) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
// I haven't made the saveDate method yet...
You have a few things wired up incorrectly. You need to make the date picker the text field's inputView at the point you create the text field, not after the user taps a button that isn't added until the user puts focus on the text field.
You also don't need a Done button and a Save Date button. Either have a single Done button or have a Save and a Cancel button.
You are also going to need to implement the textFieldDidEndEditing delegate method to remove any buttons that you add.
Your saveDate method will need to update the text field's text with the currently selected date (after converting the date to a string with an NSDateFormatter).
The action for your Done/Save/Cancel button(s) (whatever you end up doing) simply needs to call resignFirstResponder on the text field. This will dismiss the date picker. The button actions may also need to save off or revert the date's value as appropriate.

uidatepicker on uitextfield: How can I disable particular textfield keyboard and enable uidatepicker?

I'm using uidatepicker pop up while click in uitextfield, The date picker is poping up with keyboard and How can I disable particular textfield keyboard and enable uidatepicker?
Firstly set your textfield delegate if you have not already set it then in delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField isEqual:toDateTxtFld])
{
// Add this code in delegate
[yourTextField resignFirstResponder];
// ADD YOUR CODE FOR DATEPICKER Code
return NO;
}
return YES;
}
So keyboard will resign whenever you click in textfield.
If the date picker and the keyboard are popping up together, you can always call [myTextField resignFirstResponder]; to remove the keyboard if it has popped up when the user tapped on the UITextField

Resources