I have a grouped style UITableView with a HeaderView that is loaded from another .xib.
The HeaderView has 4 UITextFields in it:
2 of the fields should display the Keyboard and allow user input.
2 of the fields should display a UIPickerView and update the UITextField with selection.
The Main TableView (with the sections and rows) is filled with UITextFields as well.
The UITextField in the first section (indexpath.section = 0) displays a UIPickerView
All other UITextFields in the rest of the sections/rows should display the Keyboard
I can get the Keyboard to display correctly and dismiss when the Done button is touched for all the UITextFields that can display the keyboard.
I can get the UIPickerView to display correctly and dismiss (with Custom Save/Cancel buttons).
The problem I have is when mixing the two...
When I do the following I have a hybrid effect:
Step 1: Touch the first UITextField to begin to enter data with the Keybard.
Step 2: Enter some data in the UITextField.
Step 3: Touch a UITextField that displays a UIPickerView instead of the Keyboard.
The result is the Keyboard AND my custom UIPickerView being displayed at the same time with the Keyboard actually displaying ON TOP of my UIPicker!
Any suggestions on how to prevent this from happening?
Need to show some code, but most likely you're not calling [myTextField resignFirstResponder] just before calling the UIPickerView.
You need to resign your keyboard for your textField. From the fact that you did not do it, I am guessing that you did not resign the pickerView as well. So you need to resign both of them or they will continue to stay on the screen
Resigning the textField.
Implement UITextFieldDelegate
Implement
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}
Resigning pickerview:
Implement the picker view as your property and implement [self.yourpickerview resignFirstResponder] in textFieldDidEndEditing and in the IBAction of your custom DONE button for your picker view.
Related
My Question is : What is the best way to implement "Hiding Keyboard while PickerView (which is inside the PopOver) is Visible"?
What I Want :
I have one view which has around 15 TextFields. Out of 15 , say 7 uses UIPickerView ( that resides into UIPopOverController ) to implement the DropDown Functionality. My Problem is when the KeyBoard is Visible and I Click on the UITextField with UIPickerView, KeyBoard is not hiding. What is the best way to implement this ?
What I Tried :
I tried to implement inputView but it is only used for UIPickerView while I have UIPickerView inside UIPopOverController. I think that's why I am unable to use inputView property. According to this Question, it is Bug from Apple.
Update :
Look , I know that using textField Delegates , we can implement this. But I found inputView property of UITextField and I found it quite interesting as you have to just assign it as :
textField.inputView = pickerView;
But i have the UIPickerView inside PopOver. So How to use this inputView property of UITextField ?
Any Better Idea ?...
I guess you can not do this directly (as inputView). You can try this simple work around:
1) Replace your UITextField with UIButton and connect it to some selector (buttonClicked:). You can use UIButtonTypeCustom to be able to style it so it looks like text field (set layer's bordeColor, borderWidth, cornerRadius, etc..)
2) In this method -(void)buttonClicked:(id)sender - toggle the same UIPickerView you would use as inputView for replaced UITextField. You can make in ordinary instance variable, just remember to set delegate and dataSource. Attach it to the root view (of your UIViewController show in popover) or even UIWindow and animate it in or out (toggle) depending if it is already shown or not.
3) Update button's text (by using [button setTitle:<selected_value> forState:UIControlStateNormal]) in UIPickerViewDelegate method: `pickerView:didSelect...atIndex: - you can additionally hide picker here..
Hope this would help.
Take an object in your .h file as:
id currentTextField;
Implement this UITextField Delegate Method in your .m file:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = textField;
}
Don't forget to make the txtField.delegate = self for all the UITextFields in your file.
Now when you receive the tap on your pickerView Button, first add a statement on that button's click method:
[currentTextField resignFirstResponder];
All the best!!!
I'm pretty new to iPhone development, so please excuse my ignorance. I've googled this a bit and so far come up with nothing useful. Here is what I would like to do:
I would like a subview to popup(with the rest of the screen showing in the background) when a UITextField is touched. The popup is a calculator UIView that I created in the IB. It seems it is better to have a popup show than a customized keyboard, due to Apple's developer guidelines.
Here is my question. How do I capture the touch on the UITextField and how do I show the subview?
I have tried things like below to show the subview, with no luck:
CustomCalculator *customCalc = [[CustomCalculator alloc] initWithNibName:#"CustomCalculator" bundle:nil];
UIViewController *calcController = [self.customCalc.view];
[self.view addSubview:calcController.view];
Use the delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//Add your subview here
return NO; //this will stop the keyboard from poping up
}
This way when someone taps the textfield, your view will popup instead of the keyboard.
Now once the user is interacting with your view, you will have to manipulate the string in the textfield.text property directly as a reaction to the User tapping buttons in your view.
Implement the UITextFieldDelegate and the method for it is
-(void) textFieldDidBeginEditing:(UITextField *)textField
The above method is fired when you touch the UITextField. You may then position the UIPopoverController (I'm guessing that is what you're using to show the view in a popup) and as soon as you're done there pass the values back to the UITextField. Hence the popover's/viewcontroller presented's delegate should be your textfield object.
EDIT: After seeing the other answer below it struck me that I forgot to tell you how to stop the keyboard from showing. Just make this the first line in the method I've mentioned above:
[textField resignFirstResponder];
I have a UITableView containing in each cell a UITextField. When the user clicks on the UITextField, as expected, the keyboard will popup.
I have implemented in my delegate tableView: didSelectRowAtIndexPath: method to dismiss the keyboard with resignFirstResponder sent to the last UITextField used.
Everything works ok if the cell with the last UITextField used is displayed.
Now, if I scroll down to bottom of the tableview and press on a row, then the resignFirstResponder is sent to a hidden UITextField and will not hide the keyboard. It doesn't throw an error also.
How can I hide the keyboard in such cases?
Have a look at UITextFieldDelegate and put the
[textField resignFirstResponder];
method in one of the callback functions. I particulary prefer:
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
Did you try calling endEditing:YES method of the UITextField?
-(IBAction)hidekey:(id) sender{
[textField resignFirstResponder];
}
if you using Interface builder then checked outlet of text field and in .m file use above function and define for that text field. It will work.
Please use this tutorial to Create a return Key for UIKeyboardTypeNumberPad ! This should save a lot of time for you.
I have the following code...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//some code here...
NSInteger theTag = textField.tag; //I set the tag to 5 in IB
if (theTag == 5) {
//self.showDatePicker;
[textField resignFirstResponder];
}
}
The problem is, the keyboard never disappears. Another thing to note is that I have some other methods that move the view up and down based on the position of the textfield selected. Maybe that's messing up my Responder, but I just don't understand why the keyboard won't go away.
Also, I might just be doing this all wrong. I want this textField, when pressed, to hide the keyboard and show a date picker. Should this be in a different method?
If you're targeting iOS 3.2+, I'd suggest looking into UITextField's inputView property. You can assign a custom view (i.e. a data picker) to be displayed when the text field becomes the first responder instead of the keyboard.
I have a custom cell with a uitextfield inside. I want to hide the keyboard when the user touch the screen, I put a custom uibutton over my tableView, and in the touch up inside event, I call
-(IBAction) hideKeyBoard
{
[customcell.textfield resignFirstResponder];
}
is it the right way to hide the keyboard with a uitableview because it don't works
No, a UIButton over your tableview is going to obstruct touches to the table, and views with alpha less than something like 0.1.
One method would be to subclass UITableView and override touchesBegan to detect a touch. From there, you have many options for how to deal with resigning first responder, notification, delegate method, reference to the text field.