3

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

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
iOSAppDev
  • 2,755
  • 4
  • 39
  • 77

2 Answers2

4

Your delegate should implement - (BOOL)textFieldShouldReturn:(UITextField *)textField method

zakovyrya
  • 9,579
  • 6
  • 39
  • 28
3

May be you can add another piece of code to your function:

if([[textView text] isEqualToString:@"\n"]) {
    [textView resignFirstResponder];
    return NO;
}
Nava Carmon
  • 4,523
  • 3
  • 40
  • 74
  • I tried this solution. But if user wants to write on next line then how can I handle this? – iOSAppDev Mar 19 '11 at 15:24
  • next line is @"\r" or @"\r\n" you can add this check too, but as far as i know if you turn on the check wrap text, it will automatically go to the next line in case the user reached the end of the textview rectangle – Nava Carmon Mar 19 '11 at 16:51