How textview work like as textfield? - iphone

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;
}

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];
}

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..

UITextView resign first responder on 'Done'

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

Resign keyboard on 'Done' button of keyboard

I want to resign keyboard on click of 'Done' button on keyboard. How can i do this? I have following code =>
textView.returnKeyType = UIReturnKeyDone;
I have this code for limiting number of characters in textview =>
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(range.length > text.length)
{
return YES;
}
else if([[textView text] length] >= MAX_LENGTH_TEXTVIEW && range.length == 0)
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
How can i resign keyboard on clicking "Done" button of keyboard? If i click on Done , it's not resigning instead it is going to next line (i.e \n ). Is there any delegate method for "Done" method or so? i am not getting method for "Done" from apple documentation.
plz help me ....thanks in advance....
Your delegate should implement - (BOOL)textFieldShouldReturn:(UITextField *)textField method
May be you can add another piece of code to your function:
if([[textView text] isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}

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.