クリックされたときに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)