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;
}
Related
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;
}
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 textfield called searchBox and am trying to get rid of the keyboard if the user either clicks on Done.
Is there an IBAction that I need to know to do this?
You have to make your viewcontroller an UITextFieldDelegate and write in
viewDidLoad:
[searchBox setDelegate:self];
And then write:
- (BOOL)textFieldShouldReturn:(UITextField *)textfield
{
[searchbox resignFirstResponter];
return YES;
}
In your view controller:
- (void)viewDidLoad
{
// ...
searchBox.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[searchBox resignFirstResponder];
return YES;
}
See the UITextFieldDelegate documentation for details (you might want to return NO instead of YES in my example).
I'm trying to get the keyboard to disappear when the screen is touched, a question that is answered all over stackoverflow. I was able to get the keyboard to disappear when the enter key was pressed thanks to a thread here. I'm not having luck on the background touch resigning the first responder. The method is being entered, I have an NSLog in the method saying, "in backgroundTouched" but the keyboard is still there.
I've tried making the UIView a UIControl class so I could use the touch event.
journalComment is a UITextView.
-(IBAction)backgroundTouched:(id)sender
{
[journalComment resignFirstResponder];
NSLog(# "in backgroundTouched");
}
I've also tried having a invisible button under everything that calles the backGroundTouched method. I think it maybe that I'm missing something in interface builder, but I'm not sure what.
Thank you for any help!
This is what works for the done button:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:#"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
I found the following code works best with my text view (not text field) without the delegate methods:
first you set up a tap gesture recognizer onto your view :
- (void)viewDidLoad{
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tap:)];
tapRecognizer.delegate = self;
[self.view addGestureRecognizer:tapRecognizer];
}
and then in your tap method :
- (void)tap:(id)sender
{
// use to make the view or any subview that is the first responder resign (optionally force)
[[self view] endEditing:YES];
}
this should allow your keyboard to be dismissed when you anywhere on the view.
Hope this helps
Try this. We had this problem eariler, but eventually found the right solution.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[yourtextfield resignFirstResponder];
// you can have multiple textfields here
}
This should resolve the problem with the keyboard not dissapearing when pushing the background.