MatterGoal
MatterGoal

Reputation: 16430

Adding custom NSControl and NSCell to a xib

I've created a really simple NSControl with relative NSCell to make some tests. To add this control on a Window I add it via "Interface Builder" dragging an NSView, and than changing its class to MyControl.

Here my code:

THE NSControl

@implementation MYControl


+ (void)initialize
{
    if (self == [MYControl class])
    {
        [self setCellClass: [MYCell class]];
    }
}

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
    }

    return self;
}


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


@end

THE NSCell

@implementation MYCell

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{  
    /*
    [[NSGraphicsContext currentContext]saveGraphicsState];
    [[NSColor redColor]set];
    [NSBezierPath fillRect:cellFrame];
    [[NSGraphicsContext currentContext]restoreGraphicsState];*/
}
@end

If I remove from NSControl class every references to MyCell it works (but obviously showing nothing)otherwise, starting the app i get some errors:

<Error>: kCGErrorFailure: CGSShapeWindow 

<Error>: kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.

_NXPlaceWindow: error setting window shape (1000)

<Error>: kCGErrorFailure: CGSShapeWindow

_NSShapeRoundedWindowWithWeighting: error setting window shape (1000)

What i wrong ? how can i setup correctly a Custom NSControl via XCode4/IB ? From documentation i read something about IB Palette but i think that I can't use it in Xcode 4.0

EDIT:

Adding the NSControl programmatically with initWithFrame it works

Upvotes: 2

Views: 1059

Answers (1)

macawm
macawm

Reputation: 142

I believe I have finally found the answer.

The -cellSize method must be overridden in your NSCell subclass. After gobs of stack tracing I discovered that this method will return (40000, 40000) as your cell size if it is not overridden thus creating the sizing errors that we have seen. Since I have special needs in my NSCell that require the cell to occupy the entire NSControl's drawing area I simply used the following.

- (NSSize)cellSize {
    return self.controlView.bounds.size;
}

Hope this helps your situation.

Upvotes: 3

Related Questions