NSTextField enter to trigger action

折月煮酒 提交于 2020-01-28 05:40:46

问题


I've been looking all over for a simple example of how to have an action (or button) be triggered when the enter key is hit in the text field.

Should I subclass the text field? Would I need to set a delegate to call the action I need? Is there a way to catch the event in my main window controller class?

If you could even just point me to the right direction that would be great. Thanks.


回答1:


To get action after you hit enter just write an IBAction in your window controller and connect to your text field. If you want more like your method should be called when you focus on text field and leave the text field you need to set delegate(See here).




回答2:


The site that is referenced in the comments to the accepted answer ... is now "SUSPENDED" by the webhost, and Google cache doesn't have the screenshot that contains the key step.

So, here's an alternative solution I found that:

  1. works perfectly
  2. does NOT require subclassing

For some keys (Enter, Delete, Backspace, etc), Apple does NOT invoke the normal controlTextDidEndEditing: etc methods. Instead, Apple does individual selectors for each magic key - but there's a method you can use to intercept it.

Apple's official docs here:

https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextEditing/TextEditing.html#//apple_ref/doc/uid/TP40009459-CH3-SW31

...but in case that vanishes / gets moved, add this method into your delegate:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{           
    BOOL retval = NO;

    if (commandSelector == @selector(insertNewline:)) {

        retval = YES; // causes Apple to NOT fire the default enter action

        // Do your special handling of the "enter" key here

    }

    NSLog(@"Selector = %@", NSStringFromSelector( commandSelector ) );

    return retval;  
}

In my case, I wanted to override the backspace key too - running the app with this method, I got output saying that the selector was "deleteBackward:", so I added another if-statement in there to react to that.




回答3:


Just connect an IBAction of your controller class with a text field. The action should be called with you hit enter or leave the text field with Tab or Mouse.




回答4:


For the curious, here's how to implement -controlTextDidEndEditing:(NSNotification)obj (if you don't care about responding to just the enter key)

AppDelegate.h:

// Conform to the protocol to avoid compiler warnings
@interface myClass : NSObject <NSTextFieldDelegate>

// Link up to the interface elements you want to trigger
@property (weak) IBOutlet NSTextField *myTextField;
@property (weak) IBOutlet NSButton *myButton;

// Create an action linked to myButton
- (IBAction)myButtonPressed:(id)sender;

// Advertise that you implement the method
- (void)controlTextDidEndEditing:(NSNotification *)obj;

AppDelegate.m:

@synthesize myTextField = _myTextField;
@synthesize myButton = _myButton;

// Make yourself the delegate
- (void)applicationDidFinishLoading:(NSNotification *)aMessage {
    _myTextField.delegate = self;
}

// NSTextField objects send an NSNotification to a delegate if
// it implements this method:

- (void)controlTextDidEndEditing:(NSNotification *)obj {
    if ([[obj object] isEqual:_textField]) {
        [self myButtonPressed:nil]; // nil = sender
    }
}

Worked like a charm for me.




回答5:


The best thing to do would be to use control + drag to the action you've already created, so pressing return will also trigger the action.



来源:https://stackoverflow.com/questions/2635132/nstextfield-enter-to-trigger-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!