textViewDidChange is not call when change UITextView.inputView?

懵懂的女人 提交于 2019-12-21 20:47:33

问题


I drag a UITextView and hook up delegate with File's Owner.
The textViewDidChange is called only with default keyboard, but nothing happen when input text from my keyboard.

How to enable delegate of UITextView when set its inputView to a custom keyboard?

Here is my code

ViewController.m

-(void)viewDidLoad{
    self.myKeyboard = [[[NSBundle mainBundle] loadNibNamed:@"MyKeyboard" owner:nil options:nil] objectAtIndex:0];
    [self.myKeyboard setTextView:self.textView];
}

#pragma mark TestViewDelegate
    - (void)textViewDidChange:(UITextView *)textView{
        NSLog(@"TextDidChange: %@",textView.text);
    }

MyKeyboard.h

@interface MyKeyboard : UIView
@property (weak,nonatomic) id<UITextInput> textView;
-(void)setTextView:(id<UITextInput>)textView;
@end

MyKeyboard.m

 -(void)setTextView:(id<UITextInput>)textView{
        _textView = textView;
        if ([textView isKindOfClass:[UITextView class]])
            [(UITextView *)textView setInputView:self];
        else if ([textView isKindOfClass:[UITextField class]])
            [(UITextField *)textView setInputView:self];
    }

回答1:


In your custom keyboard, you probably change text inside textView by using method setText:, but changes made in this method doesn't call a textViewDidChanged callback.
Read more in documentation.

textViewDidChange:
Discussion

The text view calls this method in response to user-initiated changes to the text. This method is not called in response to programmatically initiated changes.

So, you should manually call this method from your keyboard.



来源:https://stackoverflow.com/questions/16115344/textviewdidchange-is-not-call-when-change-uitextview-inputview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!