在 NSTextField 中按下 Enter 键时执行操作? [英] Execute an Action when the Enter-Key is pressed in a NSTextField?

查看:44
本文介绍了在 NSTextField 中按下 Enter 键时执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个小问题.我想在 NSTextField 中按下 Enter 键时执行一个方法.用户应该能够输入他的数据,并且应该在他按下回车键后立即执行计算方法.

I have a small problem right now. I want to execute a method when the Enter key is pressed in a NSTextField. The user should be able to enter his data and a calculation method should be executed as soon as he hits the enter key.

推荐答案

您可以通过设置文本字段的操作来做到这一点.在 IB 中,将文本字段的选择器连接到您的控制器或任何呈现您想要使用的 IBAction 的对象.

You can do this by setting the text field's action. In IB, wire the text field's selector to your controller or whatever object presents the IBAction you want to use.

要在代码中设置它,向 NSTextField 发送一个 setTarget: 消息和一个 setAction: 消息.例如,如果您在代码中在控制器对象上设置此项,并且您的 textField 出口称为 myTextField:

To set it in code, send the NSTextField a setTarget: message and a setAction: message. For example, if you're setting this on your controller object in code, and your textField outlet is called myTextField:

- (void)someAction:(id)sender
{
  // do something interesting when the user hits <enter> in the text field
}

// ...

[myTextField setTarget:self];
[myTextField setAction:@selector(someAction:)];

这篇关于在 NSTextField 中按下 Enter 键时执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆