如何用`NSControl`和`NSActionCell`编写一个自定义控件 [英] How do I write a custom control with `NSControl` and `NSActionCell`
问题描述
我在这里寻找一个最小可行的例子。我已经googleing和阅读了几天,我找不到一个单一的资源是最新的。
我的NSControl工作正常,但一旦我添加
+(Class)cellClass {
return [MYCustomCell class];
}
我得到这个输出,我的窗口停止绘制正确
<错误> ;: kCGErrorFailure:CGSShapeWindow
/ pre>
<错误> ;: kCGErrorFailure:设置断点@ CGErrorBreakpoint因为他们被记录。
_NXPlaceWindow:错误设置窗口形状(1000)
<错误> ;: kCGErrorFailure:CGSShapeWindow
_NSShapeRoundedWindowWithWeighting:错误设置窗口形状(1000)
我已经阅读了我可以找到的关于这个主题(包括苹果文档)的一切,但一切似乎都模糊不清,来正确实现。
解决方案我相信我最终找到了答案。
-cellSize方法必须在NSCell / NSActionCell子类中重写。堆栈跟踪后,我发现这个方法将返回(40000,40000)作为您的单元格大小,如果它没有被覆盖,从而创建我们已经看到的大小错误。因为我在NSActionCell子类中有特殊的需求,需要单元格来占据整个NSControl的绘图区域,所以我只是使用了以下内容。
- (NSSize)cellSize {
return self.controlView.bounds.size;
}
希望这有助于您的处境。
I'm looking for a minimal viable example here. I've been googleing and reading up for days now and I can't find a single resource that is up to date.
My NSControl works ok, but as soon as I add
+ (Class)cellClass { return [MYCustomCell class]; }
to it I get this output and my window stops to draw properly
<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)
I've read everything I could find about this subject (including the Apple docs), but everything seems far to vague to come to a proper implementation. I would really appreciate it if somebody could point me in the right direction (proper code example or article).
解决方案I believe I have finally found the answer.
The -cellSize method must be overridden in your NSCell/NSActionCell 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 NSActionCell subclass 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.
这篇关于如何用`NSControl`和`NSActionCell`编写一个自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!