frameworker

October 13, 2006

Making an NSButton first responder

Filed under: Uncategorized — frameworker @ 7:47 am

I have an interface in which I want the first responder to be whatever control the user clicks on, even if it’s a button. I thought I was missing something, but I guess not. At tonight’s Cocoaheads meeting at Apple, Scott Stevenson suggested that I subclass NSButton and override NSButton’s click method to tell the window to set the first responder to self. It worked!

I used “mouseDown” instead of “performClick” which had initially looked like the thing to do. And the window method is “makeFirstResponder.” For some reason I’d expected it to be “setFirstResponder.” It finally dawned on me to look around the API a bit 🙂

// MyButton.h

#import

@interface MyButton : NSButton
{
}

– (void)mouseDown:(NSEvent *)theEvent;

@end

// MyButton.m

#import “MyButton.h”

@implementation MyButton

– (void) mouseDown:(NSEvent *) theEvent
{
[super mouseDown:theEvent];
BOOL isFirstResponder = [[self window] makeFirstResponder:self];
}

@end

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: