NSTextField 的自动补全方法
我在触发 NSTextfield 的 complete:
方法时遇到问题。
现在我可以使用 @distinctUnionOfObjects
(删除数组重复项的绝佳方法)从文本字段创建一个不同的名称数组,现在我可以使用以下方法发回此文本字段的自动完成功能:- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView 补全:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
但是,这种方法不是自动的,我必须在数据输入期间按 ESC 按钮才能弹出文本字段的自动完成建议。
我在这里搜索并发现了一些对我来说没有意义的例子。
简短问题: 是否有任何使用 NSTexfields 委托(如 controlDidChanged
或类似的方法)的方法可以更轻松、更清晰地执行此操作?
我只是对 nstextview 使用 complete:
方法感到困惑。
I'm having problem triggering complete:
method for NSTextfield.
for now I can make an distinct array of names from a textfield using @distinctUnionOfObjects
( awesome method to remove duplicates of an array ) and now I can send back autocompletion for this textfield using:- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
But, this method is not automatic and I have to press ESC button to pop the autocompletion suggestion up for the textfield during data entry.
I searched here and found some examples that make no sense for me.
Short Question:
Is there any method using NSTexfields delegates like controlDidChanged
or something like that to do this more easily and clearly ?
I just confuse using complete:
method for nstextview.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不建议复制整个字符串。这对于您的情况来说没问题,但如果您对大型文本文件使用自动完成功能,那么您将遇到各种性能和内存问题。您可以跟踪是否正在更新。如果您有多个文本视图,您可以为 isCompleting 变量创建一个字典。
I don't recommend copying the whole string. It would be fine for your case, but if you're using autocompletion for a large text file, then you'll have all kinds of performance and memory issues. You can just keep track of whether or not you are in the middle of an update. If you have multiple textviews, you could make a dictionary for the isCompleting variables.
当您的文本字段委托获取
controlTextDidChange:
时,您可以在 字段编辑器。这是当您按 ESC 或 F5 时调用的方法。棘手的部分是,当导航完成菜单时,它将导致再次发送
controlTextDidChange:
消息(尽管不更改实际字符串),这将创建无限循环。当您已经处于完成状态时,您将需要某种标志来阻止complete:
被调用。例如,您可以跟踪用户对字符串所做的最后更改,并将其与字段编辑器的当前值进行比较;如果没有用户发起的更改,则不会导致完成:When your text field delegate gets
controlTextDidChange:
, you can callcomplete:
on the Field Editor. This is the method that gets called when you press ESC or F5.The tricky part is that when the completion menu is being navigated, it will cause
controlTextDidChange:
messages to be sent again, (although without changing the actual string) which will create an infinite loop. You will need some kind of flag to stopcomplete:
from being called when you are already in the middle of a completion. For example, you can keep track of the last change the user made to the string and compare it with the current value of the field editor; if there's no user-initiated change, don't cause completion: