Re: Subclassing NSSearchFieldCell
Re: Subclassing NSSearchFieldCell
- Subject: Re: Subclassing NSSearchFieldCell
- From: Stefan Fisk <email@hidden>
- Date: Thu, 3 Nov 2005 01:44:01 +0100
I just came up with a most evil solution! =)
If you override the control's -initWithDecoder: in the following
manner you can convert the cell to your custom subclass:
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
/* if the cell is not an instance of the control's cell class, but
a super class of it */
if ([[self cell] class] != [[self class] cellClass]
&&
[[[self class] cellClass] isSubclassOfClass:[[self cell]
class]])
{
NSCell oldCellCopy = [self->_cell copy];
NSCell newCell = [ConvertObjectIntoSubclass(oldCellCopy,
[AnimatedSearchFieldCell class]) autorelease];
[newCell awakeAfterUsingCoder:aDecoder]
[self setCell:newCell];
NSDeallocateObject(oldCellCopy);
}
}
return self;
}
#import "/usr/include/objc/objc-class.h"
/*
* Creates a copy of object that is an instance of subclass.
* The copy will have a retain count of one.
* The instance variables that subclass adds will be filled with
garbage.
*/
id ConvertObjectIntoSubclass(id object, Class subclass) {
NSCParameterAssert([subclass isSubclassOfClass:[object class]]);
/* Creates a copy of object with the subclass's instance size */
id copy = NSCopyObject(object, subclass->instance_size - [object
class]->instance_size, NSZoneFromPointer(object));
NSCAssert(copy, @"Failed to copy object.");
/* Turns the object into an instance of subclass */
copy->isa = subclass;
return copy;
}
This requires that the cell subclass does it's initialization in -
awakeAfterUsingCoder:, which isn't exactly how things should be.
Still, it's not, from the cell's point of view at least, to odd, and
should also work when decoding instances the normal way. Most
certainly this will break under some circumstances, so ymmv.
_______________________________________________
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