textViewDidChange is not call when change UITextView.inputView?
Asked Answered
L

1

7

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];
    }
Lathan answered 20/4, 2013 at 0:39 Comment(1)
I don't see the delegate set anywhere. Have you run textView.delegate = self?Shadchan
E
13

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.

Eelgrass answered 24/3, 2014 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.