Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I needed to have an <code>NSTextField</code> call a delegate function upon clicking it today, and thought this basic code might be useful. Note that <code>NSTextField</code> 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.</p> <pre><code>MouseDownTextField.h: #import &lt;Appkit/Appkit.h&gt; @class MouseDownTextField; @protocol MouseDownTextFieldDelegate &lt;NSTextFieldDelegate&gt; -(void) mouseDownTextFieldClicked:(MouseDownTextField *)textField; @end @interface MouseDownTextField: NSTextField { } @property(assign) id&lt;MouseDownTextFieldDelegate&gt; delegate; @end MouseDownTextField.m: #import "MouseDownTextField.h" @implementation MouseDownTextField -(void)mouseDown:(NSEvent *)event { [self.delegate mouseDownTextFieldClicked:self]; } -(void)setDelegate:(id&lt;MouseDownTextFieldDelegate&gt;)delegate { [super setDelegate:delegate]; } -(id)delegate { return [super delegate]; } AppDelegate.h: @interface AppDelegate &lt;MouseDownTextFieldDelegate&gt; ... @property IBOutlet MouseDownTextField *textField; ... AppDelegate.m: ... self.textField.delegate = self; ... -(void)mouseDownTextFieldClicked:(MouseDownTextField *)textField { NSLog(@"Clicked"); ... } ... </code></pre> <p>If you're building with 10.5 SDK, don't have the protocol inherit from <code>NSTextFieldDelegate</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload