Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textViewDidChange is not call when change UITextView.inputView?

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];
    }
like image 730
strong Avatar asked Apr 20 '13 00:04

strong


1 Answers

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.

like image 179
ArtFeel Avatar answered Oct 12 '22 04:10

ArtFeel