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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/995758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:04:25  来源:igfitidea点击:

Execute an Action when the Enter-Key is pressed in a NSTextField?

cocoaxcode

提问by TalkingCode

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.

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

回答by Jason Coco

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.

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

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:

要在代码中设置它,请向 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:)];

回答by M.Shuaib Imran

You have to do only this

你只需要这样做

For some keys (Enter, Delete, Backspace, etc)

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

self.textfield.delegate = self;

and then implement this method

然后实现这个方法

- (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
} 

回答by Raff

In your delegate (NSTextFieldDelegate), add the following:

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

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

回答by drdrdrdr

The Swift 3version of @M.ShuaibImran's solution:

@M.ShuaibImran 解决方案的Swift 3版本:

First subclass your ViewController to: NSTextFieldDelegate

首先将您的 ViewController 子类化为: NSTextFieldDelegate

class MainViewController: NSViewController, NSTextFieldDelegate {
  ...
} 

Assign the textField delegate to the ViewController in your viewDidLoad():

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

self.textField.delegate = self

Include the NSTextFieldDelegate method that handles keyboard responders:

包括处理键盘响应者的 NSTextFieldDelegate 方法:

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
}

回答by Vikas Bansal

It's very easy and you can do it directly from UI editor

这非常简单,您可以直接从 UI 编辑器中完成

  1. Right click the Text Feild, drag Action reference to the button as shown below in the screenshot
  1. 右键单击文本字段,将操作引用拖到按钮上,如下图所示

enter image description here

在此处输入图片说明

  1. Now it will give you some option as shown in screen shot below, you need to select perform click
  1. 现在它会给你一些选项,如下面的屏幕截图所示,你需要选择执行点击

enter image description here

在此处输入图片说明

  1. Now it should look like this
  1. 现在它应该是这样的

enter image description here

在此处输入图片说明

Note:The event will be raised as soon as you press Tabor Enterkey. In case you want the action should only be raised when user presses the Enterkey then you have to do a setting. Go to the Attribute inspectorand change the Actionproperty to Send on Enter onlyas shown in screen shot below

注意:只要您按下TabEnter键,就会引发事件。如果您希望仅在用户按下Enter键时触发操作,则您必须进行设置。转到Attribute inspector并将Action属性更改Send on Enter only为如下屏幕截图所示

enter image description here

在此处输入图片说明

回答by iljawascoding

NSTextFieldDelegate's – control:textView:doCommandBySelector:is your friend.

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

Look for the insertNewline:command.

寻找insertNewline:命令。

回答by Daniel Morris

In Interface Builder - click on your NSTextField, go to the connections editor, drag from selector to your controller object - you're actions should come up!

在界面生成器中 - 单击您的 NSTextField,转到连接编辑器,从选择器拖动到您的控制器对象 - 您应该执行操作!

回答by Sathish Kumar

Best way to do that is to bind the NSTextFieldvalue with NSStringproperty.

最好的方法是将NSTextField值与NSString属性绑定。

For Example,define a method:

例如,定义一个方法:

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

add bindings:

添加绑定:

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

Enter any text and hit the return key, setTextFieldStringwill be called.

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