我創建了一個非常簡單的NSControl,並使用相對NSCell進行一些測試。 要在窗口上添加此控件,我通過「Interface Builder」拖動NSView將其添加到該窗口中,並將其更改爲MyControl
。將自定義NSControl和NSCell添加到xib中
這裏我的代碼:
的的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
的的NSCell
@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/IB正確設置自定義NSControl?從文檔我讀一些關於IB調色板,但我認爲我不能在Xcode 4.0
編輯使用它:
與initWithFrame添加的NSControl編程它的工作原理
我不知道你的文章或項目中是否有錯字,但是你的實現文件有MYCell(大寫字母「Y」),並且在你的MyControl代碼中引用了MyCell。 – rdelmar
@rdelmar這是一個錯字。我已經糾正了它:)謝謝 – MatterGoal
@MatterGoal你對這件事有任何結論嗎?我有一個非常類似的問題。如果你已經懂得了什麼,請在我的問題上留下一個答案:-) http://stackoverflow.com/questions/11618525/how-do-i-write-a-custom-control-with-nscontrol-and- nsactioncell – Wukerplank