Resign keyboard on 'Done' button of keyboard - iphone

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

Related

UITextField ResignFirstRespond not working in iphone?

I am designed one form with four UITextFields. First One for entering numbers, Second for entering name, third for choosing date and last one for choosing time. First textfield using Numberpad it will hide by using done button on the keyboard. If the user when enter into second (Name textfield) from first (number textfield) the first keyboard hide perfectly. If the user entering name and select date textfield am showing the datepicker in Actionsheet, when the user pick the date and hit done/cancel button i hide the actionsheet. But, the name textfield keyboard don't dismiss from the screen. I have tied below code, it is not working. Can anyone please suggest the idea for this clarification? Thanks in advance.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == numberTextfield)
{
[textField setInputAccessoryView:keyboardToolbar];
}
else if(textField == nameTextfield)
{
}
else if(textField == dateTextField)
{
[self showdateActionSheet];
[nameTextfield resignFirstResponder];
[dateTextField resignFirstResponder];
}
else if(textField == timeTextField)
{
[timeTextField resignFirstResponder];
[nameTextfield resignFirstResponder];
}
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField // If the user select Return key it is dismissing fine
{
[nameTextfield resignFirstResponder];
return YES;
}
-(BOOL) textFieldShouldEndEditing:(UITextField *)textField
{
[nameTextfield resignFirstResponder];
return YES;
}
-(void) DateDone
{
[nameTextfield resignFirstResponder];
}
-(void) timeDone
{
[nameTextfield resignFirstResponder];
}
Gopinath. Please try my following code,
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == dateTextField )
{
[nameTextfield resignFirstResponder];
[self showdateActionSheet];
[dateTextField resignFirstResponder];
}
else if( textField == timeTextField)
{
[nameTextfield resignFirstResponder];
[self ShowTime];
[timeTextField resignFirstResponder];
}
return YES;
}
I am sure i will help you.
Just do this:
self.view.editable = NO;
Link: How can I programmatically link an UIView or UIImageView with an event like “touch up inside”.

UITextView keyboard hidding problem

I want to hide the keyboard after typing the content into the UITextView but i can not do this by default like UITextField. Is there any way to hide the keyboard, Rightnow i put the button which can help me out.If any default way to hide it then please tell me.
Use following code for hide the keyboard from text view when user enter "\n"(new line) means press "Done" or "return" button.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
Example1 and Example2.
[textView resignFirstResponder];
Here is your detail answer

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.