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

查看:965
本文介绍了在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 a setTarget:消息和 setAction:消息。例如,如果你在代码中在你的控制器对象上设置它,并且你的textField outlet被称为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天全站免登陆