在 NSTextField 中按下 Enter 键时执行操作?

我现在有个小问题。我想在 NSTextField 中按下 Enter 键时执行一个方法。用户应该能够输入他的数据和计算方法应该尽快执行,只要他点击输入键。

45967 次浏览

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

要在代码中设置它,请向 NSTextField 发送一条 setTarget:消息和一条 setAction:消息。例如,如果用代码在控制器对象上设置此属性,而 textField 出口名为 myTextField:

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


// ...


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

在 Interface Builder 中-点击你的 NSTextField,进入连接编辑器,从选择器拖动到你的控制器对象-你的动作应该出现!

NSTextFieldDelegate-control:textView:doCommandBySelector:是你的朋友。

查找 insertNewline:命令。

在您的委托(NSTextFieldDelegate)中,添加以下内容:

-(void)controlTextDidEndEditing:(NSNotification *)notification
{
// See if it was due to a return
if ( [[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement )
{
NSLog(@"Return was pressed!");
}
}

你只能这么做

对于某些键(Enter、 Delete、 Backspace 等)

self.textfield.delegate = self;

然后实现这个方法

- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
NSLog(@"Selector method is (%@)", NSStringFromSelector( commandSelector ) );
if (commandSelector == @selector(insertNewline:)) {
//Do something against ENTER key


} else if (commandSelector == @selector(deleteForward:)) {
//Do something against DELETE key


} else if (commandSelector == @selector(deleteBackward:)) {
//Do something against BACKSPACE key


} else if (commandSelector == @selector(insertTab:)) {
//Do something against TAB key


} else if (commandSelector == @selector(cancelOperation:)) {
//Do something against Escape key
}
// return YES if the action was handled; otherwise NO
}

实现此目的的最佳方法是将 NSTextField值与 NSString属性绑定。

例如,定义一个方法:

(void)setTextFieldString:(NSString *)addressString {}

添加绑定:

[textField bind:@"value" toObject:self withKeyPath:@"self.textFieldString" options:nil];

输入任何文本并按回车键,将调用 setTextFieldString

这是非常容易的,你可以直接从 UI 编辑器做到这一点

  1. 右键单击“文本字段”,将“操作”引用拖到按钮上,如下图所示

enter image description here

  1. 现在它将提供一些选项,如下面的屏幕截图所示,您需要选择执行单击

enter image description here

  1. 现在应该是这个样子

enter image description here

注意: TabEnter键即可引发事件。如果你想要的动作应该只引发当用户按下 Enter键,然后你必须做一个设置。转到 Attribute inspector并将 开拍属性更改为 Send on Enter only,如下面的屏幕截图所示

enter image description here

斯威夫特3/4/5版的@M. ShuaibImran 解决方案:

首先将 ViewController 子类化为: NSTextFieldGenerate

class MainViewController: NSViewController, NSTextFieldDelegate {
...
}

将 textField 委托分配给 viewDidLoad ()中的 ViewController:

self.textField.delegate = self

包括处理键盘响应器的 NSTextFieldCommittee 方法:

func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
if (commandSelector == #selector(NSResponder.insertNewline(_:))) {
// Do something against ENTER key
print("enter")
return true
} else if (commandSelector == #selector(NSResponder.deleteForward(_:))) {
// Do something against DELETE key
return true
} else if (commandSelector == #selector(NSResponder.deleteBackward(_:))) {
// Do something against BACKSPACE key
return true
} else if (commandSelector == #selector(NSResponder.insertTab(_:))) {
// Do something against TAB key
return true
} else if (commandSelector == #selector(NSResponder.cancelOperation(_:))) {
// Do something against ESCAPE key
return true
}
    

// return true if the action was handled; otherwise false
return false
}