2012-05-15 17 views
2

我創建了一個非常簡單的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編程它的工作原理

+0

我不知道你的文章或項目中是否有錯字,但是你的實現文件有MYCell(大寫字母「Y」),並且在你的MyControl代碼中引用了MyCell。 – rdelmar

+0

@rdelmar這是一個錯字。我已經糾正了它:)謝謝 – MatterGoal

+0

@MatterGoal你對這件事有任何結論嗎?我有一個非常類似的問題。如果你已經懂得了什麼,請在我的問題上留下一個答案:-) http://stackoverflow.com/questions/11618525/how-do-i-write-a-custom-control-with-nscontrol-and- nsactioncell – Wukerplank

回答

3

我相信我終於找到了回答。

-cellSize方法必須在您的NSCell子類中重寫。在堆棧追蹤之後,我發現如果未覆蓋此方法,將返回(40000,40000)作爲您的單元格大小,從而創建我們所看到的大小錯誤。由於我在NSCell中有特殊的需求需要單元佔據整個NSControl的繪圖區域,所以我簡單地使用了以下內容。

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

希望這可以幫助你的情況。

+0

我已經提出這個作爲與蘋果的錯誤#12217884 – macawm