当用户在 NSSearchField 中按 Enter 键时调用函数?
我很惊讶我找不到这个简单任务的答案。我只希望用户输入文本,按 Enter 键,然后让应用程序知道他/她输入的内容。我该怎么做?
I am surprised that I can't find the answer to this simple task. I just want the user to type in text, press enter, and have the application know what s/he typed. How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSSearchField
支持操作目标机制,因此只需将其挂钩到目标/操作即可。例如,假设您在应用程序委托中声明了以下操作:在 Interface Builder 中,按住 Ctrl 键并将搜索字段拖动到应用程序委托对象,然后选择
searchAnswer:
操作。在其实现中,使用-stringValue
获取用户在搜索字段中键入的文本,例如请注意,默认情况下,搜索字段也会在用户暂停/停止键入时发送操作。如果您希望它仅在用户键入 Enter 时发送操作,请在搜索字段属性检查器窗口中选中
发送整个搜索字符串
复选框。NSSearchField
supports the action target mechanism, so simply hook it to a target/action. For instance, suppose you have the following action declared in your application delegate:In Interface Builder, ctrl-drag your search field to the application delegate object and choose the
searchAnswer:
action. In its implementation, use-stringValue
to obtain the text typed by the user into the search field, e.g.Note that by default the search field sends the action when the user pauses/stops typing, too. If you want it to send the action only when the user types Enter, choose the
Sends Whole Search String
checkbox in the search field attributes inspector window.