如何使 NSSearchField 在自动完成时发送操作?
这个问题看起来很简单,但我已经尝试了我能想到的一切,并用谷歌搜索了几个小时。
我有一个可以自动完成的 NSSearchField ,基本上复制了 Apple 的 SearchField 示例代码。我已经在 IB 中关闭了“发送整个搜索字符串”,因为我不想在用户完成文本输入之前进行搜索,并且不想进行多次搜索(它们很昂贵)。
当用户在字段中键入内容时,当他们按 Enter 键时,指定他们接受当前的自动完成功能,我希望触发 NSSearchField 的操作。相反,它似乎只是填写自动完成,然后用户必须再次按 Enter 键才能触发操作。基本上,想象一下开始在 Safari 中输入 URL,它会自动完成,然后按 Enter 键开始加载页面(触发操作)。他们不需要再次按 Enter 键即可开始加载页面。
我尝试过但没有成功的事情:
- control:textView:commandSelector:,寻找 insertNewline:。当他们按 Enter 完成自动完成
- 覆盖 controlTextDidEndEditing: 时,它不会被触发。与上面相同
有什么想法吗?谢谢!
This question seems straightforward but I've tried everything I can think of, and Googled for hours.
I have an NSSearchField that does autocomplete, basically copying Apple's SearchField sample code. I have turned off "Sends Whole Search String" in IB because I don't want to do the search until the user has finalized their text entry, and don't want to do multiple searches (they are expensive).
As the user types in the field, when they press enter, specifying that they accept the current autocompletion, I want the action for the NSSearchField to fire. Instead, it just seems to fill-in the autocompletion, then the user has to press enter a second time for the action to fire. Basically, think of starting to type in a URL in Safari, it autocompletes, and pressing enter starts loading the page (firing the action). They don't need to press enter a second time to start loading the page.
Things I've tried without success:
- control:textView:commandSelector:, looking for insertNewline:. It doesn't get fired when they are pressing enter to finish the autocompletion
- Overriding controlTextDidEndEditing:. Same as above
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想出了如何进行这项工作。
您需要覆盖 NSTextViews 的 NSFieldEditor。
要提供重写版本,在 NSWindow 的委托中:
_mlFieldEditor 是一个实例变量。定义如下:
关键部分是 [super insertCompletion...] 之后的 NSReturnTextMovement。
第一部分将更改它,以便键入空格键不会执行自动完成,这是我的评论:
如何防止 NSSearchField 覆盖输入的字符串使用第一个自动完成列表条目?
I figured out how to make this work.
You need to override the NSFieldEditor for the NSTextViews.
To provide an overridden version, in the NSWindow's delegate:
_mlFieldEditor is an instance variable. Here is the definition:
The key part is the NSReturnTextMovement after the [super insertCompletion...].
The first part will change it so that typing the space key won't perform autocompletion, which was a comment I had made on:
How to prevent NSSearchField from overwriting entered strings using the first autocompletion list entry?