Re: NSSlider and NSSliderCell
Re: NSSlider and NSSliderCell
- Subject: Re: NSSlider and NSSliderCell
- From: Chris Suter <email@hidden>
- Date: Tue, 16 Jan 2007 12:32:26 +1100
On 16/01/2007, at 12:16 PM, Murat Konar wrote:
On Jan 15, 2007, at 3:55 PM, Michael Watson wrote:
I've tried eleventeen different ways of setting the cell class so
the slider subclass creates and uses the custom subclassed cell,
but it always uses the standard NSSliderCell, not the custom cell.
[snip]
When the slider wakes up from the nib...
Stop right there.
What happens when your slider is reanimated from a nib is that it
is unarchived from the bits that you indirectly archived when you
made your nib. Since Interface Builder only knows about NSSlider
and NSSliderCell, that's what gets archived. That means your
sliders values, and crucially, the cell it uses are restored
directly from the archived nib data, not created dynamically by
your subclass.
You have a couple of things you could try:
(a) write your own Interface Builder Palette that makes your
subclass directly available to Interface Builder
(b) in your subclass' initWithCoder: method, call the super
implementation. At this point your cell should be an instance of
NSSliderCell. Dump it and substitute your own.
Option (a) is probably more trouble than it is worth. Option (b)
may or may not be more work than its worth (I've never tried it
myself, so there may be potholes I haven't anticipated).
_murat
Actually setCellClass is documented as a third approach but,
unfortunately, it doesn't work as documented (which I have reported).
From Apple's docs:
==
If you have a custom cell subclass that you would like to substitute
for the class of a cell object in a nib file, you should set the cell
class in the awakeFromNib method (NSNibAwaking protocol). You cannot
change the class programmatically after the cell object has been
unarchived from the nib and instantiated, which occurs immediately
after awakeFromNib returns. If you are going to be using your custom
cell frequently, consider creating your own Interface Builder palette
containing the cell.
==
I believe it works with some classes but it certainly doesn't work
with NSTextField which is where I've encountered this problem.
I've worked around the problem by overriding cell. Something like this:
- (NSCell *)cell
{
NSCell *cell = [super cell];
if (![cell isKindOfClass:[MyCustomCellClass class]) {
NSCell *newCell = [[[MyCustomCellClass alloc] initTextCell:[cell
stringValue]] autorelease];
[newCell setFont:[cell font]];
// etc.
[self setCell:cell = newCell];
}
return cell;
}
You could also stick this in awakeFromNib.
I also continue to set the cell class in the initialize method in
case it ever gets fixed.
- Chris
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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