更改UITextView.inputView时,不会调用textViewDidChange吗? [英] textViewDidChange is not call when change UITextView.inputView?
问题描述
我拖动UITextView
并将其与File's Owner
挂钩.
仅使用默认键盘调用textViewDidChange
,但是从键盘输入文本时什么也不会发生.
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.
将UITextView
设置为自定义键盘时,如何启用UITextView
的委托?
How to enable delegate of UITextView
when set its inputView
to a custom keyboard?
这是我的代码
ViewController.m
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
MyKeyboard.h
@interface MyKeyboard : UIView
@property (weak,nonatomic) id<UITextInput> textView;
-(void)setTextView:(id<UITextInput>)textView;
@end
MyKeyboard.m
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];
}
推荐答案
在自定义键盘中,您可能使用方法setText:
更改了textView
中的文本,但是在此方法中所做的更改不会调用
在文档中了解更多信息.
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:
讨论
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.
这篇关于更改UITextView.inputView时,不会调用textViewDidChange吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!