Re: NewbieQ: Why can't I have a customview as an initalResponder??
Re: NewbieQ: Why can't I have a customview as an initalResponder??
- Subject: Re: NewbieQ: Why can't I have a customview as an initalResponder??
- From: email@hidden
- Date: Tue, 22 Jan 2002 12:25:27 -0500
On Monday, January 21, 2002, at 04:30 PM, Andrew Platzer wrote:
By default, unless you have full keyboard UI turned on, only controls
like NSTextField and NSBrowser can accept first responder status.
That's why it jumps to a text field on loading the nib. If your control
is similar in that it always wants to accept key focus, override the
method -[NSView needsPanelToBecomeKey] to return YES.
This method is poorly named (historical reasons) and we are looking at
making it a bit more obvious what to override to get the behaviour you
want and adding to the documentation.
I attempted to use this magic cocoa bean by adding a
needsPanelToBecomeKeyView method to the test code (see below), and the
custom view still does not become the initial responder. Bug or feature??
Thanks for helping,
email@hidden
------------------------- CustomView.h --
#import <Cocoa/Cocoa.h>
@interface CustomView : NSView
{ // This view paints itself red unless it is firstResponder
} // If it is firstResponder, it paints itself green.
- (BOOL)acceptsFirstResponder;
- (BOOL)becomeFirstResponder;
- (void) keyDown: (NSEvent *) event;
- (BOOL)resignFirstResponder;
@end
------------------------- CustomView.m --
#import "CustomView.h"
@implementation CustomView
- (BOOL) needsPanelToBecomeKey // Attempt to use the magic
firstResponder cocoa bean
{
return YES;
}
- (void) drawRect: (NSRect) rect
{
NSRect bounds = [self bounds];
if ([[self window] firstResponder] == self) // Be green if we are
first responder
[[NSColor greenColor] set];
else
[[NSColor redColor] set]; // Be red if we are NOT the first
responder
[NSBezierPath fillRect: bounds];
}
- (BOOL )acceptsFirstResponder
{
NSLog (@"acceptsFirstResponder!");
return YES;
}
- (BOOL)becomeFirstResponder
{
NSLog (@"becomeFirstResponder!");
[self setNeedsDisplay: YES];
return YES;
}
- (BOOL)resignFirstResponder
{
NSLog (@"resignFirstResponder!");
[self setNeedsDisplay: YES];
return YES;
}
- (void) keyDown: (NSEvent *) event
{
NSString *input = [event characters];
NSLog (@"keyDown!");
if ([input isEqual: @"\t"]) {
[[self window] selectNextKeyView: nil];
}
}
@end