praveena praveena - 7 days ago 5
Objective-C Question

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.

Answer

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