UITextView textview应该终止它从未调用 [英] UITextView textviewshouldendediting never called
问题描述
我有这样的UITextView设置:
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.
推荐答案
设置返回键类型时,它不会改变文本视图的行为.在 Return
上,它将在文本视图中添加换行符.因此,如果您不希望文本视图为多行,则可以捕获 \ n
和 resignFirstResponder
.
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
and resignFirstResponder
.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ( [text isEqualToString:@"\n"] ) {
[textView resignFirstResponder];
}
return YES;
}
在旁注中, textViewShouldEndEditing:
在您退出第一响应者状态后被调用.
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 is here
.
这篇关于UITextView textview应该终止它从未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!