becomeFirstResponder not called
becomeFirstResponder not called
- Subject: becomeFirstResponder not called
- From: Hasan Diwan <email@hidden>
- Date: Wed, 19 Jun 2002 02:16:42 -0400
I'm slowly working my way through the Hilegass book and am having
problems with the code below. Namely, the method becomeFirstResponder is
never called. I have no idea why this would be the case. In any case,
here's the code:
// BigLetterView.m
#import "BigLetterView.h"
@implementation BigLetterView
- (void)setBgColor:(NSColor *)c {
[c retain];
[bgColor release];
bgColor=c;
[self setNeedsDisplay:YES];
}
- (NSColor *)bcColor {
return bgColor;
}
- (void)setString:(NSString *)c {
[c retain];
[string release];
string=c;
NSLog (@"The string is %@", string);
[self setNeedsDisplay:YES];
}
- (void)drawStringCenteredIn:(NSRect)r {
NSPoint stringOrigin;
NSSize stringSize;
stringSize = [string sizeWithAttributes:attributes];
stringOrigin.x = r.origin.x + (r.size.width - stringSize.width)/2;
stringOrigin.y = r.origin.y + (r.size.height - stringSize.height)/2;
[string drawAtPoint:stringOrigin withAttributes:attributes];
}
- (NSString *)string {
return string;
}
- (void)drawRect:(NSRect) r {
NSRect bounds = [self bounds];
[bgColor set];
[NSBezierPath fillRect:bounds];
[self drawStringCenteredIn:bounds];
if ([[self window] firstResponder] == self) {
[[NSColor blackColor] set];
[NSBezierPath strokeRect:bounds];
}
}
- (void)keyDown:(NSEvent *)event {
NSString *input = [event characters];
if ([input isEqual:@"\t"]) {
[[self window] selectNextKeyView:nil];
return;
}
if ([input isEqual:@"\031"]) {
[[self window] selectPreviousKeyView:nil];
return;
}
[self setString:input];
NSLog (@"keyDown @%", [event characters]);
[self setNeedsDisplay:YES];
}
- (void)prepareAttributes {
attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:
[NSFont fontWithName:@"Helvetica"
size:75]
forKey:NSFontAttributeName];
[attributes setObject:[NSColor redColor]
forKey:NSForegroundColorAttributeName];
}
- initWithFrame:(NSRect)rect {
if (self = [super initWithFrame:rect]) {
NSLog(@"Initializing application");
[self prepareAttributes];
[self setBgColor:[NSColor yellowColor]];
[self setString:@" "];
}
return self;
}
- (BOOL)acceptsFirstResponder {
NSLog(@"Accepting");
return YES;
}
- (BOOL)resignFirstResponder {
NSLog(@"Resigning");
[self setNeedsDisplay:YES];
return YES;
}
- (BOOL)becomeFirstRespnder { // NEVER CALLED
NSLog (@"Becoming");
[self setNeedsDisplay:YES];
return YES;
}
- (void)dealloc {
[string release];
[bgColor release];
[attributes release];
[super dealloc];
}
@end
---
// BigLetterView.h
#import <Cocoa/Cocoa.h>
@interface BigLetterView : NSView
{
NSColor *bgColor;
NSString *string;
NSMutableDictionary *attributes;
}
- (void)prepareAttributes;
- (void)drawStringCenteredIn:(NSRect)bounds;
- (void)setBgColor:(NSColor *)c;
- (NSColor *)bcColor;
- (void)setString:(NSString *)c;
- (NSString *)string;
@end
---
Hasan Diwan
OpenPGP KeyID: 0xBE42DCA6
Fingerprint: 1CB0 47E3 0A24 DAC1 DCCA 4225 F166 40C2 BE42 DCA6
http://hasandiwan.net/~hdiwan/gpg.key
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.