iPhone设置UITextView委托破坏自动完成
我有一个 UITextField,我想通过以下方式启用自动完成:
[self.textView setAutocorrectionType:UITextAutocorrectionTypeYes];
这正常工作,除非我给 UITextView 一个委托。设置委托后,自动完成功能将停止工作。委托只有以下方法:
- (void)textViewDidChange:(UITextView *)textView
{
self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
int left = LENGTH_MAX -[self.textView.text length];
self.characterCountLabel.text = [NSString stringWithFormat:@"%i",abs(left)];
}
有谁知道如何同时启用自动完成和委托集?
谢谢!
特里斯坦
I have a UITextField that I would like to enable auto completion on by:
[self.textView setAutocorrectionType:UITextAutocorrectionTypeYes];
This works normally, except when I give the UITextView a delegate. When a delegate is set, auto complete just stops working. The delegate has only the following method:
- (void)textViewDidChange:(UITextView *)textView
{
self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
int left = LENGTH_MAX -[self.textView.text length];
self.characterCountLabel.text = [NSString stringWithFormat:@"%i",abs(left)];
}
Does anyone know how to have both auto complete enabled and a delegate set?
Thanks!
Tristan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可能会破坏自动完成功能,因为您同时修改
UITextView
中的文本。It's probably breaking autocompletion because you're simultaneously modifying the text in the
UITextView
.该问题是由于每次更改文本时可能会修改文本(即调用替换方法)而引起的。解决方案是仅在必要时调用替换方法。
The problem was caused by doing something that potentially modifies the text every time it was changed (ie calling replace method). The solution was to only call the replace method when it was necessary.
明白了:您所需要做的就是从界面 (.h) 文件中删除 UITextViewDelegate。
您仍然可以将委托设置为笔尖中的textView。
奇怪吧?为我工作,我希望它也能解决您的问题。
Get this: All you need to do is remove UITextViewDelegate from your interface (.h) file.
You can still set the delegate to the textView in the nib.
Odd, right? Worked for me, I hope it would solve your problem as well.