3

I am a little stumped as to how to instruct a programmatically created NSSegmentedControl to use a subclass instance of an NSSegmentedCell.

If I want to use a subclasses NSSegmentedCell on an NSSegmentedControl built using IB it would be as simple as doing the following:

  1. Drag an NSSegmentedControl into the NSView
  2. Click through to the NSSegmentedCell
  3. In the inspector assign the class definition to the subclass (e.g. myCustomCell)

Job done.

However, when programmatically creating an NSSegmentedControl as in the following simplified example, I don't see how to subclass the cell...

-(void)creatSegmentControl {

    if (!mySegmentControl)
        mySegmentControl = [[NSSegmentedControl alloc] 
                               initWithFrame:NSMakeRect(0,0 400,20)];

    [mySegmentControl setSegmentCount:2];
    [mySegmentControl setLabel:@"First" forSegment:0];
    [mySegmentControl setLabel:@"Second" forSegment:0];
    [mySegmentControl setTarget:self];
    [mySegmentControl setAction:@selector(segmentClicked:)];
}

NSSegmentedControl does not appear to have a method for defining the class to use for it's segment cell instances.

As usual, any and all help appreciated.

Update

Tried implementing [mySegmentControl setCellClass:[myCustomCell class] but that didn't work either. I was thinking that maybe it inherited the ability to set it's cell class like other AppKit controls. :-(

This must be possible though... somehow...

Hooligancat
  • 3,588
  • 1
  • 37
  • 55
  • The SDK documentation is little help here. Perhaps the solution is to load your segmented control from a nib that contains just the control? – Seamus Campbell Aug 05 '10 at 01:17
  • @Seamus, thanks for the swift response. I could put it into a NIB, but I already have a lot of nib files in my application so I am a little hesitant to add NIB's just for the sake of a subclass. You are right, the docs offer little to zero help in this area. – Hooligancat Aug 05 '10 at 02:18
  • Did you look at the `cell` property of `NSControl` ? – silicontrip Mar 12 '21 at 07:08

2 Answers2

0

Kinda late, but wouldn't overwrite cellClass work?

+ (Class)cellClass
{
    return [YourCustomCellClass class];
}
Taiki
  • 629
  • 5
  • 13
  • Never too late, because there is always someone else looking for the answer and the discussion here made me go looking in the right spot, to find it. – silicontrip Mar 12 '21 at 22:03
0

The property cellClass is in the deprecated category.

You need to make an instance of your custom class and set NSControl's cell property, before anything else (yes NSSegmentedControl inherits from NSControl)

    NSSegmentedControl* oSegment = [[NSSegmentedControl alloc] init];
    QPDFSegmentedCell* csell = [[QPDFSegmentedCell alloc] init];
    oSegment.cell = csell;
silicontrip
  • 906
  • 7
  • 23