NewbieQ: Why can't I have a customview as an initalResponder??
NewbieQ: Why can't I have a customview as an initalResponder??
- Subject: NewbieQ: Why can't I have a customview as an initalResponder??
- From: email@hidden
- Date: Sat, 19 Jan 2002 23:13:01 -0500
Hi,
Newbie here. I want to have a custom view be the initial responder in
a simple window, but I can't make it work. As a test, I created a window
in IB with just a custom view, and then set the Window's
initialFirstResponder outlet to the CustomView (after setting the
customview's class to my custom view). But the @#$@#% custom view does
not come up as the first responder.
If I throw a textfield onto the window, IT becomes the initalResponder,
even though IB says that my custom view is the first responder.
My custom view does nothing but paint it self red when its NOT the
responder, and green when it is. When the window opens the view comes up
red, until I click on it, and then it turns green.
I have NSLog messages that show that the CustomView receives an
acceptsFirstResponder message (which it answers YES), but it never gets
a becomeFirstResponder message until I click on it.
The test code is tiny and is attached to the bottom of this message. Is
there some law against custom views being the first responder or am I
just missing some magic cocoa beans??
Thanks,
email@hidden
------------------------- CustomView.h --
#import <Cocoa/Cocoa.h>
@interface CustomView : NSView
{
}
- (BOOL)acceptsFirstResponder;
- (BOOL)becomeFirstResponder;
- (void) keyDown: (NSEvent *) event;
- (BOOL)resignFirstResponder;
@end
------------------------- CustomView.m --
#import "CustomView.h"
@implementation CustomView
- (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