how to resign keyBoard as a firstResponder from a

2020-04-22 01:19发布

I want to resign the keyboard from the text view as a first responder.I want as soon as user hits the return key after editing in a textView the keyboard should resign.But I am not sure how should I do this???

4条回答
我想做一个坏孩纸
2楼-- · 2020-04-22 02:07

If your class is set as the delegate for that textview just do the following in your class code

- (void)textViewTextDidEndEditing:(UITextView *)textView {
    [textView resignFirstResponder];
}
查看更多
Ridiculous、
3楼-- · 2020-04-22 02:10

Hope this code helps to you.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{    
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}

use UITextViewDelegate protocol and yourTextViewName.delegate = self;

查看更多
放荡不羁爱自由
4楼-- · 2020-04-22 02:13

Make sure the text field has been set as a delegate and add the following code to your implementation file:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[myTextField resignFirstResponder];
return YES;

}

查看更多
老娘就宠你
5楼-- · 2020-04-22 02:22
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{
  if([text isEqualToString:@"\n"])
  [textView resignFirstResponder];
  return YES;
}

yourTextView.delegate=self; Also add, in '.h' file

If you didn't add "if([text isEqualToString:@"\n"])" you can't edit

查看更多
登录 后发表回答