我有一個自定義的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)
除了那個之外,它不是在控制檯日誌中另外說些什麼嗎?像「無法識別的選擇器發送到實例0x ...」等 – Yuji 2011-01-21 05:53:33