Matt Stein
Matt Stein

Reputation: 3053

Why can't I add custom NSButtonCell to NSView?

I'm attempting to create a custom view that contains a play/pause button, and I'll attach any number of these to an NSWindow. I started by creating my own NSView and just drawing out the various pieces, then subclassing the play/pause button as an NSView (baby steps).

This all worked fine until I decided my button needed to extend NSButtonCell rather than an NSView. The following (from TimeSlipView.m) fails miserably, and I can't seem to figure out why:

playPauseButton = [[TimeSlipViewButton alloc] init];
[playPauseButton setButtonType:NSMomentaryPushInButton];
[self addSubview:playPauseButton];

I get a compile error and this warning for that last line: "Incompatible pointer types sending 'TimeSlipViewButton *__strong' to paremeter of type 'NSView *'".

I have a feeling I've misunderstood something very basic, and that for some reason I can't just pass addSubview: my NSButtonCell from within an NSView.

TimeSlipView.h

#import <Cocoa/Cocoa.h>
#import "TimeSlipViewButton.h"

@interface TimeSlipView : NSView {
    TimeSlipViewButton *playPauseButton;
    NSView *timerText;
    NSView *clientText;
    NSView *projectText;
    NSView *taskText;
}

@end

TimeSlipViewButton.h

#import <Cocoa/Cocoa.h>

@interface TimeSlipViewButton : NSButtonCell

@end

Upvotes: 1

Views: 734

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50129

A Cell Is no View and thus cannot be used as such! what you do doesn't work
You try exactly that when adding it as a subview

Cells are a (legacy) concept where views were too expensive.

The were/are used by some controls (like NSButton) to handle the actually drawing.


the Button CONTAINS a button cell
It ISNT a button cell, it IS a NSView


what you might wanna do is give a stock NSButton a specific ButtonCell that has custom drawing options. There are good tutorials out there for given existing NSButtons/NSSegmentedCells/NSTextFields custom NSCells

Upvotes: 1

Related Questions