UITextView resign first responder on 'Done' - iphone

I have been searching around the web for about an hour now, and cannot find any code to help me with this.
I have a UITextView that I need to resign first responder of when the user presses the 'Done' button on their keyboard.
I have seen code floating around the internet like this:
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
But that will not work for a UITextView.
Simply put,
How can I tell when the user presses the done button on the keyboard?

Implement the shouldChangeTextInRange: delegate method.
Use below approach and the solution work only with #"\n" (new line character).
//In you *.h file make sure you add
#interface v1ViewController : UIViewController <UITextViewDelegate>
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myTextField.delegate = self;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[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;
}

You can use the delegate method
- (void)textViewDidEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
}
You have to set your controller as the delegate for the TextView.
For more methods you can have a look here: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html%23//apple_ref/doc/uid/TP40006897

Related

How to hide Textbox Keyboard when "Done / Return" Button is pressed Xcode 4.2

I created a Text Field in Interface Builder. I set it's "Return Key" to Done. This is a one line only input (so it wont need multiple lines).
How do I hide the virtual keyboard when the user taps the done button?
Implement the delegate method UITextFieldDelegate, then:
- (void)viewDidLoad {
[super viewDidLoad];
self.yourIBtextField.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
UITextView does not have any methods which will be called when the user hits the return key.
Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.
There might be other ways but I am not aware of any.
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;
}
Make category on UIViewController with next method
- (void)hideKeyboard
{
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
}

UITextField strange behaviour on resignFirstResponder

Already the second day and cannot figure out the problem,
I've UITabelView with Custom UICellViews, each custom UICellView consists of UILabel and UITextField.
Custom UICellView object allocs UITextField and UILabel in its init method and are released in dealloc.
The number of custom UICellViews in UITableView is 6.
The user scenario is following
When user clicks from from 1 to 5 UITextFields virtual keyboard opens and user types some text
When user clicks on the 6th UITextField if virtual keyboard is active, it should be hidden, and if it is hidden it shall not be displayed.
As implement UITextFieldDelegate protocol in my UIViewController class and set the delegate of each UITextField to self.
My delegate methods are following
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
[textField resignFirstResponder];
return NO;
}
}
-(BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField {
/* Some code */
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
All the functions are properly !
So now, the virtual keyboard is never get hidden, why this happens ?
PS. Similar code has worked on iPhone but this issue exists on iPad.
You need to know which textfield was last used! so you can do [lastUsedTextField resignFirstResponder]
There is a dirty, but working trick.. you can make your textfield the new active UITextField and call resignFirstResponder in the next cycle immediately:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag != 6) {
return YES;
} else {
// this will schedule keyboard dismissal for the current text field
dispatch_async(dispatch_get_main_queue(), ^{
[textField resignFirstResponder];
});
return YES; // -> make this one active
}
}
did you seted action for textField?
[YourTextField addTarget:self action:#selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
PS set any selector for any ControlEvent

How textview work like as textfield?

I am developing one app, in that app I use TextView to enter the data.
How to quit the TextView and resign the keyboard?
use this delegate method with return key deduction to resign textview.see here
make sure u have
urTextView.delegate=self;
and in ur viewcontroller.h file
#interface urViewController : UIViewController<UITextViewDelegate> {
- (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;
}
For resign the keyboard of text-view you have to handle the delegate method of UITextview.
Below is the code for resign the keyboard of UITextview.
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[textView resignFirstResponder];
return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
{
}
return YES;
}

How can I perform a selector when the user hits the Send button on the iPhone keyboard?

I have a UITextView. When the user hits the Send key I want to be able to automatically perform a selector. How can I do this? I know UITextField has
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Try this out:
- (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"]) {
// If the Done button was pressed, resign the keyboard.
[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;
}
Hope that Helps!
you can use
[self performSelector:#selector(aMethod) withObject:nil afterDelay:0.1];
in
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
return YES;
}
Hope it helps you..

In textView when I click on done button the keyboard is not resigning

I'm getting problem in resigning the keyboard after clicking done button. I'm using textView
-(BOOL)textViewShouldReturn:(UITextView *)textView
{
if(textView == addressView)
{
if(isNotif)
{
[self setViewMovedUp:NO];
}
textView.text= [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[addressView resignFirstResponder];
}
return YES;
}
Instead of keyboard resigning the cursor is coming to new line in the text field.
Please help me.
Thank You
Praveena.
Fixed KingOfBliss's answer:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == messageInput) {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
}
return YES;
}
Use the following delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == YourTextField){
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
}
return YES;
}
Usually in textView the return key is use to add \n to the text, so its better to add some other button to top of the UItextView and code the resigning function there.
EDIT:
There is no such delegate -(BOOL)textViewShouldReturn:(UITextView *)textView
this will happen if the ViewController that is above this textView in the view hierarchy is not the delegate of this textView. If it is not then the ViewController will never get the message textViewShouldReturn. In the viewController after the subView (the UITextView) is created.
aTextView.delegate = self;
To check to make sure it is getting called add this to your function and test
NSLog(#"resigning first responder");
this will test to see if this function is even getting called
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
was enough in my case.