6

我是 iOS 开发的新手。

我已经像这样编写了头文件

@interface TextView : UITextView <UITextViewDelegate, UIPopoverControllerDelegate>

TextView.h 中。

实现文件代码如下:

- (BOOL)textViewShouldBeginEditing:(TextView *)textView
{
    ZWLog(@"called should begin edit");
    return YES;
}

- (void)textViewDidBeginEditing:(TextView *)textView
{
    ZWLog(@"called did begin edit");
}

- (BOOL)textViewShouldEndEditing:(TextView *)textView
{
    ZWLog(@"called should end editing");
    return YES;
}

- (void)textViewDidEndEditing:(TextView *)textView
{
    ZWLog(@"view did end edit");
    return YES;
}

- (BOOL)textView:(TextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //my own code
    return YES;
}

- (void)textViewDidChange:(TextView *)textView
{
   //my own code
}

当我开始输入一个字符时UITextView,我得到了回复

  • textViewShouldBeginEditing.
  • textViewDidBeginEditing.
  • shouldChangeTextInRange.
  • textViewDidChange.

但我没有得到textViewDidEndEditingor的任何回应textViewShouldEndEditing。你知道为什么这些没有被调用吗?

感谢您的回答。

4

3 回答 3

9

textViewDidEndEditing 在 textfield 退出其第一响应者时调用,当键盘消失时。

于 2013-09-04T07:18:56.607 回答
3

确保您已从 .xib 正确链接您的委托

并按原样使用以下方法,看看它会被调用

-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    ZWLog(@"textViewShouldEndEditing");
    return TRUE;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    ZWLog(@"shouldChangeTextInRange");
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return FALSE;
    }

    // For any other character return TRUE so that the text gets added to the view
    return TRUE;
}
于 2013-09-04T07:19:17.100 回答
0

确保您的委托与您的 TextView 附加在您的 XIB 文件中(如果它在 XIB 文件中)。

于 2013-09-04T09:08:06.357 回答