Substituting instance of cell subclass for instance of superclass
Substituting instance of cell subclass for instance of superclass
- Subject: Substituting instance of cell subclass for instance of superclass
- From: James Walker <email@hidden>
- Date: Fri, 11 Nov 2016 17:34:03 -0800
I want to replace (for example) an instance of NSButtonCell with an
instance of my own subclass of NSButtonCell on the fly. Now, I know
that's not a common thing to want to do; normally, you'd edit the nib to
use whatever cell class you want. But I'd like to be able to do it to
an NSAlert, for which I don't have a nib.
So, my thought was to first make an instance of my cell class that
copies all the state of the NSButtonCell instance, and then use
-[NSControl setCell:]. The documentation on setCell: does say:
"Use this method with great care as it can irrevocably damage the
affected control; specifically, you should only use this method in
initializers for subclasses of NSControl."
Anyone know what kind of damage we're talking about?
Anyway, as far as copying the cell state, I tried this:
- (id) initCopyingBaseCell: (NSButtonCell*) oldCell
{
NSData* archive = [NSKeyedArchiver
archivedDataWithRootObject: oldCell];
NSKeyedUnarchiver* coder = [[[NSKeyedUnarchiver alloc]
initForReadingWithData: archive] autorelease];
self = [super initWithCoder: coder];
if (self != nil)
{
}
return self;
}
However, the new cell has failed to copy much of the state of the old
one. Things like title, font, target, action, bezelStyle... I can
manually copy anything that I notice is missing, but I'm just wondering
why the keyed archiver approach here doesn't work.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden