NSTextField 自动完成委托方法未调用
我为 NSTextField
实现了以下委托方法来添加自动完成支持:
- (NSArray *)control:(NSControl *)control
textView:(NSTextView *)textView
completions:(NSArray *)words
forPartialWordRange:(NSRange)charRange
indexOfSelectedItem:(NSInteger *)index
问题是该方法永远不会被调用。我可以验证 NSTextField
的 delegate
设置是否正确,因为其他委托方法按其应有的方式运行。
I implemented the following delegate method for NSTextField
to add autocompletion support:
- (NSArray *)control:(NSControl *)control
textView:(NSTextView *)textView
completions:(NSArray *)words
forPartialWordRange:(NSRange)charRange
indexOfSelectedItem:(NSInteger *)index
The issue is that this method never gets called. I can verify that the delegate
of the NSTextField
is set properly because the other delegate methods function as they should.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要获取
complete:
在文本字段的 字段编辑器 在某个时刻。这就是触发完成菜单的原因,但它不会被自动调用。如果您没有将 F5 绑定到任何内容,请尝试在您的字段中键入并点击它。然后应该触发完成; Option-Esc 也可能有效。如果您想要自动完成,则需要一些工作。您可以从这样的开始:
某种标志是必要的,因为触发完成将使
NSControlTextDidChangeNotification
再次发布,这会导致调用它,触发完成,从而更改控制文本,这.. 显然,您需要在某个时候取消设置该标志。这将取决于您希望如何处理用户与自动完成功能的交互 - 对于给定的起始字符串是否可能只有一个完成,或者用户是否需要继续键入以缩小可能性(在这种情况下,您需要再次触发自动完成)?
一个简单的标志可能也无法完全做到这一点;看起来,虽然通知被重新发布,但字段编辑器的
string
不会改变——它只会响应直接键盘输入而改变。在自动完成的实现中,我发现我必须保留“最后键入的字符串”的副本,并每次将其与字段编辑器的内容进行比较。You'll need to get
complete:
called on the text field's field editor at some point. That's what triggers the completions menu, but it doesn't get called automatically. If you don't have F5 bound to anything, try typing in your field and hit that. Completion should trigger then; Option-Esc may also work.If you want auto completion, it takes some work. You could start with something like this:
Some kind of flag is necessary because triggering completion will make
NSControlTextDidChangeNotification
be posted again, which causes this to be called, triggering completion, which changes the control text, which...Obviously, you'll need to unset the flag at some point. This will depend on how you want to handle the user's interaction with autocompletion -- is there likely to only be one completion for a given start string, or will the user need to keep typing to narrow down possibilities (in which case you'll need to trigger autocompletion again)?
A simple flag might not quite do it, either; it seems that although the notification is re-posted, the field editor's
string
won't have changed -- it will only change in response to direct keyboard input. In my implementation of autocomplete, I found that I had to keep a copy of the "last typed string" and compare that each time to the field editor's contents.