我创建了一个非常简单的带有相对NSCell的NSCell来进行一些测试。要在窗口中添加此控件,我需要通过"Interface“拖动NSView,而不是将其类更改为MyControl。
这里我的代码:
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];
}
@endNSCell
@implementation MYCell
-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
/*
[[NSGraphicsContext currentContext]saveGraphicsState];
[[NSColor redColor]set];
[NSBezierPath fillRect:cellFrame];
[[NSGraphicsContext currentContext]restoreGraphicsState];*/
}
@end如果我从NSControl类中删除对MyCell的所有引用,它就会正常工作(但显然没有显示任何内容),否则,启动应用程序,我会得到一些错误:
<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)我做错什么了?如何通过XCode4 4/IB正确设置自定义NSControl?从文档中我读到了一些关于IB调色板的内容,但我认为我不能在Xcode 4.0中使用它
编辑:
以编程方式使用NSControl添加initWithFrame --它可以工作
发布于 2012-08-31 20:19:41
我相信我终于找到了答案。
必须在-cellSize子类中重写NSCell方法。在进行堆栈跟踪之后,我发现如果没有覆盖该方法,它将作为单元格大小返回(40000,40000),从而创建我们已经看到的大小大小错误。由于我的NSCell中有特殊的需求,需要单元格占用整个NSControl的绘图区域,所以我只使用了以下命令。
- (NSSize)cellSize {
return self.controlView.bounds.size;
}希望这对你的处境有帮助。
https://stackoverflow.com/questions/10597644
复制相似问题