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.