Cocoa:如何在不按 Enter 的情况下让文本字段发送操作(用户输入的内容)
我有一个简单的可可应用程序,顶部有一个可编辑的文本字段 text1,用户可以在其中输入内容,底部有一个不可编辑的文本字段/标签 text2,用于准确显示在 text1 中输入的内容。
我的问题是,当用户输入 text1 后按 Enter 键时,我只能让 text2 自行更新。有什么方法可以让 text2 自动更新,而无需用户按 Enter 键进入 text1 吗?
I have a simple cocoa application with an editable textfield text1 at the top where the user can enter stuff in as well as a non-editable textfield/label text2 at the bottom which is meant to display exactly what is typed into text1.
My problem is that I can only get text2 to update itself when the user presses enter after he types into text1. Is there any way I can have text2 automatically update itself without the user pressing enter into text1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来像是 Cocoa 绑定 的工作。您可以:
NSString
属性(在简单的情况下,它可以是您的应用程序委托);text1
绑定到该字符串属性,确保选择“连续更新值”;text2
绑定到该字符串属性。唯一需要代码的步骤是步骤 1。步骤 2 和步骤 3 可以直接在 nib 文件中配置。
Sounds like a job for Cocoa bindings. You could:
NSString
property in your model class (which, in simple cases, could be your application delegate);text1
to that string property, making sure you select ‘Continuously Updates Value’;text2
to that string property.The only step that requires code is step 1. Steps 2 and 3 can be directly configured in the nib file.
如果您希望在每次文本更改时收到通知,请在文本字段的委托中处理
controlTextDidChange:
。委托可以是响应此消息的任何对象,通常使用窗口的控制器。If you want notifications on every text change, handle
controlTextDidChange:
in text field's delegate. The delegate can be any object that responds to this message, typically the window's controller is used.