Dismiss keyboard for UITextView without using Return key - iphone

I'm using a UITextView and want to keep the normal usage of Return key, i.e. to insert a new line. But how do I dismiss the keyboard when I can't use the Return key for that?

A lot of people add a UIToolbar with a Done button on it above the keyboard. This is how the Safari app does it as well and in my opinion it is the best way to handle this situation. See a pic here.
To dismiss the keyboard, you just have to do [textField resignFirstResponder];.
Here is an okay example of how to add the UIToolbar when the keyboard shows/hides.

How the user triggers it is a design decision: another button, a swipe gesture?
When it's triggered, call:
[self.textView resignFirstResponder];

You could just use a background tap to hide the keyboard.

A good way to Dismiss keyboard.
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
//NSLog(#"ShouldBeginEditing");
if(textView == indexOneTW)
{
here use uibutton and set its target to a method which contains
[urTextView resignFirstResponder]
}
return TRUE;
}

Make sure you declare support for the UITextViewDelegate protocol.
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text
{
if([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
Enjoy Buddy with this code....

Related

how to know whether a UITextView has a focus or not

I know the becomeFirstResponder method to set a focus on a control, but how to know whether that control (for ex. UITextView) is currently having focus on it or not ?
EDIT: the problem is that I want (for example) to change the background color of the UITextView when it receives focus, where should I put the isFirstResponder call exactly ? should I use notifications in this case ?
thanks so much in advance.
if([txtView isFirstResponder]){
//Has Focus
} else {
//Lost Focus
}
This is UITextView's delegate method,
- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView{
//Has Focus
return YES;
}
This is lost focus
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:#"\n"]){
//Lost Focus
[textView resignFirstResponder];
}
return YES;
}
Use the UITextField delegate methods. When textField got focus the(bacame first responder)
- (void)textFieldDidBeginEditing:(UITextField *)textField; will be fired,
and when it lost focus
- (void)textFieldDidEndEditing:(UITextField *)textField; will be fired.

resign keyboard in UITextField

I'm working with storyboards on iOS 5 and have a simple screen that has a UITextField on it. I want to dismiss the keyboard when the user hits "Return". I followed other suggestions such as having my controller implements the UITextFieldDelegate protocol and implements textFieldShouldReturn: as follows:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[questionField resignFirstResponder];
return YES;
}
However I see that this method is never called. I have set the controller of my view in storyboarding to my custom UIViewController.
I also tried a different implementation where I create an IBAction called dismissKeyboard but oddly I can't connect the Did Exit action to my UITextField.
Any ideas?
Update : So the problem seems to be that I'm using a UITextView and not a UITextField. I wanted a large area for the text to be entered. When I change the entry field to a UITextField it works fine. Any ideas on how to make it work with a UITextView?
You should connect the dismiss method to the text field's Did End Editing action.
So the solution to resign the keyboard for a UITextView is to implement shouldChangeTextInRange.
If you want the keyboard to dismiss when tap the return key for the textview use this delegate function,
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}

How to cancel the keyboard in UISearchbar?

I'm using UISearchBar in my application and it serves as both an edit field as well as a search. So when I want to disappear the keyboard I have to use cancel button in UISearchBar but I can't have that cancel button on screen, so how could T make keyboard disappear when not used without using cancel button. Please help me as i'm new to iPhone application development.
Thanks in advance!
Use this:
[UISearchBar resignFirstResponder];
Just replace the word UISearchBar with the name of the object you have created.
Are you looking for ways you can dismiss the keyboard or how to actually do that programmatically? If programmatically, then [UISearchBar resignFirstResponder]. If you are looking for a possible way for the user to achieve that you can either make the return button on the keyboard resign its first responder status when pressed, or attach a UIGestureRecognizer to your view and set it up so that when the user clicks outside the keyboard, this keyboard goes away.
simply all you need to do is to get UITextfield control from the UISearchbar and then set UITextfield's delegate to whatever delegate that will perform -(void) textFieldShouldReturn:(UITextField *)textField
-(void)viewDidLoad{
UIView * subView;
NSArray * subViews = [searchbar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return TRUE;
}

How to Hide keyBoard on return button itself in TextView in iPhone sdk?

In my iPhone App I am using the control UItextView,
I want to hide the keyBoard on "return" Button itself, I am not using any toolBar,NavigationBar and I don't want to import any other control like button on that View.
What shold I do to hide keyboard ?
Please Help and Suggest,
Thanks
You can use the shouldChangeTextInRange delegate and look for a line break:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)string
{
if([string isEqualToString:#"\n"]){
[textView resignFirstResponder];
}
return YES;
}

iPhone: Resign Keyboard with Done button action with XIB

hi i am working on UITextview
how to Resign Keyboard, after keyboard "Done button" click Action, which XIB
Thank you
hi if you want answer for, Resign the keyboard of UITextview with default "Done" button on keyboard, then this is answer for that
- (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 NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
i followed this to resign keyboard of UITextview, if there is any concern inform me
Thank You
Create an IBAction and in IB hook it up to the UITextfield's DidEndOnExit event. Then call the [textViewName resignFirstResponder] to make the keyboard go away.
Alternatively, try using [self.view endEditing:YES] in the IBAction.
To assign the action to the Done button of a keyboard:
In these examples, myTextView is a UITextView that is defined in the header and connected in Interface Builder.
-(BOOL)textViewShouldReturn:(UITextView*)textView {
if (textView == myTextView) {
[textView resignFirstResponder];
}
return YES;
}
To assign it to an external button:
Be sure to define an IBAction in your header, then in Interface Builder, connect the button to this action for touchUpInside:
.h
-(IBAction)dismissKeyboard:(id)sender;
.m
-(IBAction)dismissKeyboard:(id)sender {
[myTextView resignFirstResponder];
}
Call resignFirstResponder on the textview that has focus in order to dismiss the keyboard.