UITextView textviewshouldendediting 从未调用过

发布于 2024-11-09 18:58:16 字数 747 浏览 4 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-11-16 18:58:16

当您设置返回键类型时,它不会改变文本视图的行为。在 Return 上,它将向文本视图添加换行符。因此,如果您不希望文本视图是多行的,则可以捕获 \nresignFirstResponder

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

    return YES;
}

附带说明一下,在您放弃第一响应者状态后,将调用 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 and resignFirstResponder.

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

    return YES;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文