问题
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:
DiscussionThe 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