UITextView textviewshouldendediting 从未调用过
我有一个像这样的 UITextView 设置:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 280, 240)];
[textView setBackgroundColor:[UIColor greenColor]];
[textView setFont:[UIFont fontWithName:@"MyriadPro-Regular" size:13]];
[textView setTextColor:[UIColor blackColor]];
[textView setText:@"Your Message...."];
[textView setBackgroundColor:[UIColor clearColor]];
[textView setDelegate:self];
[textView setReturnKeyType:UIReturnKeyDone];
我期望当用户按下键盘上的“完成”按钮时,将调用此方法(我已实现):
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSLog(@"called");
[textView resignFirstResponder];
return YES;
}
但此方法永远不会被调用..我做错了什么? 谢谢。
I have a UITextView setup like this:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 280, 240)];
[textView setBackgroundColor:[UIColor greenColor]];
[textView setFont:[UIFont fontWithName:@"MyriadPro-Regular" size:13]];
[textView setTextColor:[UIColor blackColor]];
[textView setText:@"Your Message...."];
[textView setBackgroundColor:[UIColor clearColor]];
[textView setDelegate:self];
[textView setReturnKeyType:UIReturnKeyDone];
I am expecting that when the user pressed the "Done" button on keyboard, this method will be invoked (which I have implemented):
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSLog(@"called");
[textView resignFirstResponder];
return YES;
}
But this method never get's called..What am I doing wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您设置返回键类型时,它不会改变文本视图的行为。在
Return
上,它将向文本视图添加换行符。因此,如果您不希望文本视图是多行的,则可以捕获\n
和resignFirstResponder
。附带说明一下,在您放弃第一响应者状态后,将调用
textViewShouldEndEditing:
。如果您想在文本视图中保留换行符,您应该考虑使用 文本视图的
inputAccessoryView
。此处
就是一个示例。While you set the return key type, it doesn't alter text view's behavior. On
Return
, it will add a newline to the text view. So if you don't want your text view to be multiline, you can capture the\n
andresignFirstResponder
.On a side note,
textViewShouldEndEditing:
is called after you resign your first responder status.If you want to retain newline characters in your text view, you should consider using the
inputAccessoryView
of the text view. An example for that ishere
.