5

I am trying to respond to a click within a textfield. When the click occurs, I am going to open a panel. My initial thought was to use a delegate method to respond to the click event - but I found that:

This method doesn't work:

(void)textDidBeginEditing:(NSNotification *)aNotification

This method does work, but only when I actually edit the text within the text field, not when I first click it. And - if I edit the text a second time, this method stops working:

(void)controlTextDidBeginEditing:(NSNotification *)aNotification

I could use as much detail as possible - or a code example, ideally. I know that an nstextfield inherits from NSControl, which has a mouseDown event. Is there a similar way to respond to the event with a textfield, also?

Troy Alford
  • 26,660
  • 10
  • 64
  • 82
jin
  • 2,145
  • 5
  • 27
  • 44
  • Duplicate of: http://stackoverflow.com/questions/680863/which-delegate-method-i-should-use-to-respond-to-clicks-on-a-text-field – Darron Mar 26 '09 at 01:19
  • I agreed with you, but reading the other question, it appears that it isn't. This is a Mac question; the other is an iPhone question. – Peter Hosey Mar 26 '09 at 02:07
  • Yes , I think this is my question , I put it error location before.sorry! – jin Mar 26 '09 at 13:25

2 Answers2

11

Since NSTextField inherits from the NSControl class, it also inherits the -(void)mouseDown:(NSEvent*) theEvent method.

locriani
  • 4,995
  • 2
  • 23
  • 26
  • I found that nstextfield inherits from the NSControl class , I also found that The NSController class have the method mouseDown ,but I don't know how to use it in my file, Thanks a lot . – jin Mar 26 '09 at 13:33
  • You can simply override the mouseDown event in the NSTextField class, as follows: @implementation NSTextField -(void)mouseDown:(NSEvent*) theEvent { // your code here } @end – locriani Mar 26 '09 at 21:28
  • 1
    overriding mouseDown: is not enough for editable NSTextField, as once the field editor (a NSTextView*) is activated in the text field, it is "front" and it gets the mouseDown: events! So mouseDown: will work once, but not several time in a row... And overriding mouseDown: in the field editor is *very* tricky. – Altimac Jan 29 '14 at 14:48
9

I needed to have an NSTextField call a delegate function upon clicking it today, and thought this basic code might be useful. Note that NSTextField already has a delegate and that in SDK v10.6, the delegate already has a protocol associated with it. Note that if you don't care about protocols, compiler warnings, etc., you don't need the protocol and property declarations or the getter and setter.

MouseDownTextField.h:

#import <Appkit/Appkit.h>
@class MouseDownTextField;

@protocol MouseDownTextFieldDelegate <NSTextFieldDelegate>
-(void) mouseDownTextFieldClicked:(MouseDownTextField *)textField;
@end

@interface MouseDownTextField: NSTextField {
}
@property(assign) id<MouseDownTextFieldDelegate> delegate;
@end

MouseDownTextField.m:
#import "MouseDownTextField.h"

@implementation MouseDownTextField
-(void)mouseDown:(NSEvent *)event {
  [self.delegate mouseDownTextFieldClicked:self];
}

-(void)setDelegate:(id<MouseDownTextFieldDelegate>)delegate {
  [super setDelegate:delegate];
}

-(id)delegate {
  return [super delegate];
}

AppDelegate.h:
@interface AppDelegate <MouseDownTextFieldDelegate>
...
@property IBOutlet MouseDownTextField *textField;
...

AppDelegate.m:
...
  self.textField.delegate = self;
...
-(void)mouseDownTextFieldClicked:(MouseDownTextField *)textField {
  NSLog(@"Clicked");
  ...
}
...

If you're building with 10.5 SDK, don't have the protocol inherit from NSTextFieldDelegate.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Colin
  • 2,089
  • 25
  • 34