I cannot seem to get the DONE button on the keyboard to dismiss the keyboard? Anyone know how to make it work?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
Your code, in particular resigning first responder of the textfield in -textFieldShouldReturn is fine.
I think you probably didn't hook things up correctly, possibly forgetting to set the delegate of your UITextField.
A snippet from a recent project of mine where I used a Next return key and a Done return key for two fields, toggling from the first to the second, and dismissing the second (which had a Done return key):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if(textField == self.nameTextField)
[self.descriptionTextField becomeFirstResponder];
return YES;
}
Concerning UITextView (your question specifically mentions UITextField in the title but your code has both for textfield and textview), you will need to resign first responder differently.
Since Text views are for longer text entry including multiple lines where you can also use the return key for a line break, you'll have to detect when the user changes the text.
If they try to enter a line break, then manually resign at that point.
Example follows:
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
[textView resignFirstResponder];
return YES;
}
Related
So i was reading the description about UITextView's and it says that it automatically hides the keyboard when you press the 'Return' button on the keyboard. But it wasn't working, so I tried creating an
- (IBAction)textViewReturn:(id)sender;
{
[myTextView resignFirstResponder];
}
That did not work either so i tried also doing:
- (BOOL)textViewShouldReturn:(UITextView *)textView
{
[myTextView resignFirstResponder];
return NO;
}
Not sure why the whole deal isn't working in the first place. Wondering if anyone could help?
I don't see anything in the UITextView Class Reference that says it automatically hides the keyboard when you press Return.
Also, there is no textViewShouldReturn: message in the UITextViewDelegate protocol. There is a textFieldShouldReturn: message in the UITextFieldDelegate protocol, but a text view is not a text field.
If you want it to hide the keyboard when the user presses Return, you need to do two things.
First, you need to connect some object - usually your view controller - to the text view's delegate outlet. You can do that in your nib, or you can do it in code, perhaps in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
myTextView.delegate = self;
}
Second, you need to implement the textView:shouldChangeTextInRange:replacementText: in your delegate object:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Note that if the user pastes in text containing a newline and other characters, this will not catch the newline. It will only notice when the user either taps the Return key, or when he pastes in text containing just a newline.
You can declare the delegate's class as conforming to the UITextViewDelegate protocol, in which case Xcode will helpfully autocomplete the method name. But it will work even if the class doesn't conform to the protocol.
Return button of UITextView is used to mote the cursor to the new line. But if you want to remove the keyboard on return button then please try the following code. It resigns the keyboard when return button is pressed by user. So try following code which definitely solved your problem.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Per your comment, if you want to use a UITextField instead of a UITextView, then things remain the same except that in order to hide the keyboard when you hit return, you need to implement the following function in the text field's delegate (make sure that you have set this first):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
The UISearchBar doesn't dismiss the keyboard when enter is pressed, or the user touch somewhere else.
I need to use the remove keyboard button on the bottom right of the iOS keyboard in order to remove the keyboard and invoke:
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
How can I fix it?
- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar {
[aSearchBar resignFirstResponder];
}
Also you have to set delegate for the UISearchBar: UISearchBarDelegate
It should work.
Here is sample code http://developer.apple.com/library/ios/#samplecode/ToolbarSearch/Listings/ToolbarSearch_APLToolbarSearchViewController_m.html#//apple_ref/doc/uid/DTS40009461-ToolbarSearch_APLToolbarSearchViewController_m-DontLinkElementID_9
Another option is searchBarSearchButtonClicked we can use.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// You can write search code Here
}
Add UISearchBarDelegate in .h
Also set SearchBar's object delegate to self.
you should do add UISearchBarDelegate's method:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}
EDIT : Above doesnot work then add this:
[self.view endEditing:YES];
for swift 1.2 keyboard will hide when you click finished , and there is another function for cancel but it's not good to use it as when the user click cancel he might want to search for another word ...
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
Use the following code snippet to close/hide the keyboard when return button is clicked.
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
{
[searchBar resignFirstResponder];
return NO;
}
return YES;
}
it will work -
objective c -
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
}
Swift -
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
All UITextViews on my application just won't hide the keyboard when i click the "Hide Keyboard" button on the input keyboard.
This is very strange since UITextFields behave normally and theres absolutely no delegation class linked to the TextViews, so there is absolutely no reason for this behavior.
Anyone who faced this problem and solved it?
Set the return key to something like "Done" and then use the delegate method and see if a line break occurred:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ( [text isEqualToString: #"\n"] ) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
I have a multi view app, to swap views I use:
-(IBAction) goSecond{
[self presentModalViewController:secondviewController animated:YES]
}
On this view I have a text field that the user will enter a number, but I con not get the keyboard to remove from the view. I have read several posts but can get this to work. I use:
-(IBAction) goAwayKey: (id) sender{
[sender resignFirstResponder];
}
-(IBAction) tapBack: (id) sender{
[textField1 resignFirstResponder];
}
In secondviewController.h I have
-(BOOL) disableAutomaticKeyboardDismissal;
and in secondviewController.m
-(BOOL) disableAutomaticKeyboardDismissal{
return NO;
}
I still cant get the keyboard to go away, is there anything obvious that I am missing?
Thanks for your help.
Theoretically,
[textField1 resignFirstResponder];
should work fair enough, are you sure that your methods are being called? Place a breakpoint in both methods to resign and check if they are called, that will give us some clues about your problem.
I have a simple view with a textbox and a UIButton. When I click the UIButton I simply want to hide the keyboard that is currently in the view. Is this a simple delegate I can add to the controller or something more complex?
Of the answers that exist on SO already I haven't found one that has a full solution for this context. Any help would be great!
Try something like:
[TextField resignFirstResponder];
Where TextField is the name of your text field.
This is how you hide the UITextField when you hit the return button:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
return YES;
}
This is how you hide when you hit an UIButton:
- (void)hideTextField:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
}