How can I dismiss the keyboard when the user clicks a button?
A short example for a better understanding: the user edits some text in some textfields and at the end he doesn't click "Done" or smething else on the keyboard, but he clicks on an button "Save" while the keyboard is still shown. So, how can I now dismiss the keyboard?
Thx. Regards, Daniel
You can also call [view endEditing:YES] on any view above the text field in the hierarchy. This is useful if you aren't sure which text field has first responder status. It also optionally lets a text field delegate stop the action (by returning NO from shouldEndEditing:) which is nice if you are using a delegate to do validation on the fields.
in button action write,
if([textField isFirstResponder]){
[textField resignFirstResponder];
}
if there are more textfields get the current textfield reference everytime while editing started, and resign its responder in button action.
If you have multiple text fields and don't know which one is first responder (or you simply don't have access to the text fields from wherever you are writing this code) you can call endEditing: on the parent view containing the text fields.
In Swift, in the related view controller, add this line to the button function:
self.view endEditing(true)
In addition to SriPriya's answer, please have here the Swift 2 styled version:
if numberTextField.isFirstResponder() {
numberTextField.resignFirstResponder()
}
Please enjoy...
Related
I'm having trouble finding info on manipulating the keyboard. Here's what I want to do:
When the user touches a text field, I want a keyboard to appear. However:
1) I want to move the keyboard up 30 pixels so that another view is visible south of it.
2) The keyboard should not actually edit the text field the user touched. A multiline text view will be presented for the user to edit instead.
I'm not sure of the best strategy and I don't know the methods I'll need to use or properties I'll need to set. Some sample code would be greatly appreciated.
As #HenryHarris says, you can't move the keyboard (or more appropriately shouldn't, as Apple expects it to be where they put it and will most likely reject your app if you start messing with it).
You can on the other hand, switch to a different view when they tap the text field by using something like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Present your text view, let's call it myTextView for now, and then tell it to start accepting input:
[myTextView becomeFirstResponder];
// Now return NO so that the textField does NOT become the first responder:
return NO;
}
I want to realize the function: when I click a button, a interface of keyboard will come out in a dependent interface. How can I do it? Just for iOS.
You need to add to your current view UITextField with frame = CGRectZero, create a reference to that textField in code and then on pressing button call becomeFirstResponder on textField.
You need to add an UITextField to your view and call then [myTextfield becomeFirstResponder]; Its possible to set the hidden attrribute from UITextField to YES - so the user will never see the textfield. After Keyboard input is finished you can remove the UITextField with removeFromSuperview.
Maybe a little bit dirty,but thats the solution I used often. I wonder if the SDK provide another possibility.
My app has two text fields in it's detail split. The first text field will permit the user to enter data by keyboard but the second by a picker presented inside a popover. Anyway, I want to let the keyboard (that will appear after editing the first text field) get dismissed when I press the text field that uses a popover. I used resignFirstResponder and the keyboard stays in place but it's disabled, like when I tap any keyboard key nothing happens (even the key to dismiss the keyboard doesn't work). So how can I hide the keyboard?
You should use
[textField resignFirstResponder];
not releaseFirstResponder (there is no such method)
As George said, you should call resignFirstResponder
But actually you might want to look at using the inputView property of the textField instead of using the pickerview in a popover.
You can say:
textfield.inputView = pickerview;
Why do you need the second UITextField? If you're not going to allow user input something with keyboard use UIButton (probably with custom design), show picker on press and update button's text on changes in picker
the most simplest thing to resign responders
[self.view endEditing:YES];
I have been looking for a similar questions but so far the answer doesn't fit my purposes. I have a UITableViewController inside A UINavigationController with custom UITableViewCells containing textfields inside. When I click on the textfields they become the firstResponders and when I click return they resign it. So far so good.
My problem is one of these cells is performs a Submit function. After this is done whenever I press the textfields the return button doesn't dismiss the keyboard anymore.
My question is, since I'm not releasing anything, why do these textfields stop listening to the resignfirstresponder message?
UPDATE: I finally got this working by setting an UITextfield ivar in the class, making it the first responder whenever the textfield begins editing in :
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setTextColor:[UIColor blueColor]];
focusedTextField = textField;
}
And calling [focusedTextField endEditing:YES]; when i press the submit button. Just added this after seeing some answer in SO, but I can't remember the link, sorry.
Hope it helps.
If you have declared a method for return(let's say your textfiled is called textField) use this code in your method;
[textField setUserInteractionEnabled:NO];//but this may restric you if you need to use it again
But if you use standard return property of the keyboard it may be something related to Apple's restrictions
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dismiss keyboard by touching background of UITableView
How do I make the keyboard go away when the user clicks somewhere else?
Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.
I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.
So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.
Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?
There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.
You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,
-(void) hideKeyboard {
[textFieldName resignFirstResponder];
[textFieldSurname resignFirstResponder];
for (UITextField * txtField in arrTextFields) {
[txtField resignFirstResponder];
}
}
Then, at various sections in your class, depending on the functionality required, call;
[self hideKeyBoard];
This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.
How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.
Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317