Cannot encode custom NSView
Cannot encode custom NSView
- Subject: Cannot encode custom NSView
- From: Ricky Sharp <email@hidden>
- Date: Tue, 09 Nov 2004 07:27:19 -0600
I've been trying with no success to create a custom NSView that has an IB palette. Specifically, I'm finding that my 'mGraphicColor' ivar has a value of nil at the time the encodeWithCoder: method is called.
I'd really appreciate a second pair of eyes on this. I've now restarted the project three times; each time manually coding it (i.e. I don't copy & paste from prior failed projects).
Here's the code:
[IIGraphic.h]
@interface IIGraphic : NSView <NSCopying>
{
int mGraphicType;
NSColor* mGraphicColor;
}
- (void)setGraphicType:(int)type;
- (int)graphicType;
- (void)setGraphicColor:(NSColor*)color;
- (NSColor*)graphicColor;
@end
[IIGraphic.m]
@implementation IIGraphic
- (id)init
{
if (self = [super init])
{
mGraphicType = 0;
mGraphicColor = [[NSColor blackColor] retain];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super initWithCoder:decoder])
{
mGraphicType = 0;
mGraphicColor = nil;
[self setGraphicType:[decoder decodeIntForKey:@"graphicType"]];
[self setGraphicColor:[decoder decodeObjectForKey:@"graphicColor"]];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeInt:[self graphicType] forKey:@"graphicType"];
[coder encodeObject:[self graphicColor] forKey:@"graphicColor"];
/*
[self graphicColor] is sometimes returning nil here and I cannot figure out why. When this happens, I cannot bring up the inspector in IB (it fails to load). If I force things to always use a non-nil value (e.g. replacing [self graphicColor] with [NSColor blackColor]), all works well.
*/
}
- (id)copyWithZone:(NSZone*)zone
{
IIGraphic* newCopy = [[IIGraphic alloc] init];
[newCopy setGraphicType:[self graphicType]];
[newCopy setGraphicColor:[self graphicColor]];
return newCopy;
}
- (void)dealloc
{
[mGraphicColor release];
[super dealloc];
}
- (void)drawRect:(NSRect)rect
{
switch (mGraphicType)
{
case 0:
[[NSColor redColor] set];
break;
case 1:
[[NSColor greenColor] set];
break;
default:
[mGraphicColor set];
break;
}
NSRectFill (rect);
}
- (void)setGraphicType:(int)type
{
mGraphicType = type;
[self setNeedsDisplay:YES];
}
- (int)graphicType
{
return mGraphicType;
}
- (void)setGraphicColor:(NSColor*)color
{
if (mGraphicColor != color)
{
[mGraphicColor release];
mGraphicColor = [color retain];
[self setNeedsDisplay:YES];
}
}
- (NSColor*)graphicColor
{
return mGraphicColor;
}
@end
Finally, when I first started this project, I started out with just the 'mGraphicType' iVar. I wanted to prove that everything worked. It did. It's only when I introduced the 'mGraphicColor' iVar did things fall apart.
What's really interesting is that I've successfully built another IB palette around my IIButton/IIButtonCell objects. Such objects successfully encode/decode several color objects. I'm under the impression that I'm missing something painfully obvious and I keep making the same mistakes even when starting fresh.
The only other difference is that this is an object that inherits from NSView and not NSControl/NSCell (as does IIButton/IIButtonCell). But that shouldn't make any difference as what I have coded should adhere to both the NSCoding and NSCopying protocols.
TIA,
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden