-->

adding click action to NSTextField

2020-08-09 08:08发布

问题:

How do I attach a event to a NSTextField for when I click on it? Are there any examples out there?

回答1:

NSClickGestureRecognizer *click = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(myTextFieldClicked:)];
[self.myTextField addGestureRecognizer:click];


回答2:

You can try this: It works for me.

    @interface customTextField : NSTextField
    @property (strong) id target;
    @end

    @implementation customTextField

    - (void)mouseDown:(NSEvent *)theEvent
    {
        [self sendAction:@selector(clickAction:) to:[self target]];
    }

    @end

Thanks.



回答3:

The method you are looking for is - textShouldBeginEditing:

You have to set the delegate of the NSTextField, first of all.