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
Leave a Reply