On 11/11/2016 6:27 PM, Kyle Sluder wrote:
On Fri, Nov 11, 2016, at 07:34 PM, James Walker wrote:
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.

You also have no idea if the NSAlert is already using a custom
NSButtonCell subclass—or whether it uses NSControl at all.

I can certainly check at runtime, using [view isMemberOfClass: [NSButton class]] and likewise for the cell. If things aren't what I expect, I just won't change the window, which won't be the end of the world.


Your only option here is to reimplement the alert.

--Kyle Sluder


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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/jamesw%40frameforge3d.com

This email sent to jam...@frameforge3d.com


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to