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

后端 未结 8 1933
野的像风
野的像风 2020-12-02 06:23

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 meth

相关标签:
8条回答
  • 2020-12-02 06:34

    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.

    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:)];
    
    0 讨论(0)
  • 2020-12-02 06:34

    In your delegate (NSTextFieldDelegate), add the following:

    -(void)controlTextDidEndEditing:(NSNotification *)notification
    {
        // See if it was due to a return
        if ( [[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement )
        {
            NSLog(@"Return was pressed!");
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:37

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

    First subclass your ViewController to: NSTextFieldDelegate

    class MainViewController: NSViewController, NSTextFieldDelegate {
      ...
    } 
    

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

    self.textField.delegate = self
    

    Include the NSTextFieldDelegate method that handles keyboard responders:

    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
    }
    
    0 讨论(0)
  • 2020-12-02 06:40

    Best way to do that is to bind the NSTextField value with NSString property.

    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, setTextFieldString will be called.

    0 讨论(0)
  • 2020-12-02 06:42

    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!

    0 讨论(0)
  • 2020-12-02 06:43

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

    1. Right click the Text Feild, drag Action reference to the button as shown below in the screenshot

    1. Now it will give you some option as shown in screen shot below, you need to select perform click

    1. Now it should look like this

    Note: The event will be raised as soon as you press Tab or Enter key. In case you want the action should only be raised when user presses the Enter key then you have to do a setting. Go to the Attribute inspector and change the Action property to Send on Enter only as shown in screen shot below

    0 讨论(0)
提交回复
热议问题