2011-01-21 29 views
0

我有一個自定義的NSCell用下面的代碼來顯示NSColorPanel被點擊時:NSColorPanel從的NSCell

-(void)setColorFromPanel:(NSColorPanel*)panel{ 
NSLog(@"COLOR is HERE!"); 
[self setObjectValue:[panel color]]; 
} 

- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView{ 
if(self.isColor){ 
    if([event type]==NSLeftMouseDown){ 
    NSColorPanel *panel=[NSColorPanel sharedColorPanel]; 
    [panel setColor:[self objectValue]]; 
    [panel setShowsAlpha:YES]; 
    [panel setAction:@selector(setColorFromPanel:)]; 
    [panel setTarget:self]; 
    [panel makeKeyAndOrderFront:nil]; 
    } 
    return NSCellHitContentArea; 
} 
    return NSCellHitNone; 
} 

這個代碼顯示顏色選擇器,然而,當我點擊一個顏色,它崩潰。如果刪除[panel setTarget:self]行,那麼它工作正常,儘管沒有效果(因爲我沒有收到顏色值,因爲沒有目標)。

這是堆棧跟蹤。錯誤是EXC_BAD_ACCESS。

#0 0x00007fff8667811c in objc_msgSend() 
#1 0x00007fff87081e9a in -[NSApplication sendAction:to:from:]() 
#2 0x00007fff871fa1cd in -[NSColorPanel _forceSendAction:notification:firstResponder:]() 
#3 0x00007fff871fe384 in -[NSColorPanel setColor:]() 
#4 0x00007fff8721d112 in -[NSColorPickerWheel setColor:]() 
#5 0x00007fff8721d5ae in -[NSColorPickerWheel brightnessSlider:]() 
#6 0x00007fff87081e9a in -[NSApplication sendAction:to:from:]() 
#7 0x00007fff87081df9 in -[NSControl sendAction:to:]() 
#8 0x00007fff8710d400 in -[NSCell trackMouse:inRect:ofView:untilMouseUp:]() 
#9 0x00007fff873eaf01 in -[NSSliderCell trackMouse:inRect:ofView:untilMouseUp:]() 
#10 0x00007fff8710c215 in -[NSControl mouseDown:]() 
#11 0x00007fff8702634f in -[NSWindow sendEvent:]() 
#12 0x00007fff86f5ba86 in -[NSApplication sendEvent:]() 
#13 0x00007fff86ef24da in -[NSApplication run]() 
#14 0x00007fff86eeb1a8 in NSApplicationMain() 
#15 0x00000001000029bb in main (argc=1, argv=0x7fff5fbff6a0) 
+0

除了那個之外,它不是在控制檯日誌中另外說些什麼嗎?像「無法識別的選擇器發送到實例0x ...」等 – Yuji 2011-01-21 05:53:33

回答

0

您需要發佈崩潰報告/調試器輸出的相關部分;否則,很難準確猜出出了什麼問題!你知道,我們不是靈媒。崩潰報告包含有價值的信息,爲什麼它崩潰。

也就是說,有一個開源的色彩單元here。它僅適用於啓用GC的應用程序,但如果該代碼的許可證適合您的使用,應該是一個良好開端。不要重新發明輪子。


讓我補充幾句話。 NSCellNSTableView被用作標記來繪製每行的條目並且通常被重新使用,並且不是每行都產生了NSCell。從目前的觀點來看,這在系統中擁有豐富的內存並沒有什麼意義,但在15年前NeXTStep首次設計時就有意義了。

無論如何,由於這個原因,對於一個細胞來說,將自己設置爲另一個對象的目標通常不是一個好主意,因爲該細胞往往不會持久。我想這就是導致這次崩潰的原因,即NSTableColumn已經釋放了該單元,因爲它完成了使用該特定單元。將控制器類作爲面板的目標而不是單元本身通常更安全。