Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 937
praveena Avatar asked Feb 01 '11 12:02

praveena


3 Answers

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;
}
like image 154
Jacek Kwiecień Avatar answered Oct 21 '22 11:10

Jacek Kwiecień


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

like image 38
KingofBliss Avatar answered Oct 21 '22 12:10

KingofBliss


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

like image 1
Nathan Avatar answered Oct 21 '22 13:10

Nathan