以编程方式定义 NSSegmentedCell

发布于 2024-09-12 21:58:11 字数 1181 浏览 3 评论 0原文

我对如何指示以编程方式创建的 NSSegmentedControl 来使用 NSSegmentedCell 的子类实例感到有点困惑。

如果我想在使用 IB 构建的 NSSegmentedControl 上使用子类 NSSegmentedCell,只需执行以下操作即可:

  1. NSSegmentedControl 拖入NSView
  2. 单击查看器中的 NSSegmentedCell
  3. 将类定义分配给子类(例如 myCustomCell

工作已完成。

但是,当以编程方式创建 NSSegmentedControl 时,如以下简化示例所示,我不知道如何对单元格进行子类化...

-(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 似乎没有方法定义用于其段单元实例的类。

像往常一样,任何和所有的帮助都表示赞赏。

更新

尝试实现[mySegmentControl setCellClass:[myCustomCell class],但这也不起作用。我在想,也许它继承了像其他 AppKit 控件一样设置单元类的能力。 :-(

不过这一定是可能的...不知何故...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

貪欢 2024-09-19 21:58:11

有点晚了,但不会覆盖 cellClass 工作吗?

+ (Class)cellClass
{
    return [YourCustomCellClass class];
}

Kinda late, but wouldn't overwrite cellClass work?

+ (Class)cellClass
{
    return [YourCustomCellClass class];
}
傾旎 2024-09-19 21:58:11

属性 cellClass 属于已弃用的类别。

在执行其他操作之前,您需要创建自定义类的实例并设置 NSControl 的单元格属性(是的,NSSegmentedControl 继承自 NSControl

    NSSegmentedControl* oSegment = [[NSSegmentedControl alloc] init];
    QPDFSegmentedCell* csell = [[QPDFSegmentedCell alloc] init];
    oSegment.cell = csell;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文