NSColorPanel的选择器已被调用两次

时间:2013-07-22 17:37:12

标签: objective-c cocoa nscolorpanel

我已经分享了NSColorPanel的实例,并为他设置了选择器。我用它作为背景颜色和字体颜色。当我想设置文本颜色时,一切正常,但是当我想设置背景颜色时,选择器被调用两次,背景颜色已经改变为以前的颜色。有一些代码:

- (IBAction)showColorPanel:(id)sender {
NSColorPanel *panel = [NSColorPanel sharedColorPanel];
[panel orderFront:nil];
//[panel ]
[panel setAction:@selector(changeColorForBackground:)];
[panel setTarget:self];
[panel makeKeyAndOrderFront:self];
isFontPanel = NO;
[[self getDesktopController] setFirstString];
}

选择器:

- (void)changeColorForBackground:(id)sender {
if (!isFontPanel) {
    DesktopController *desktopController = [self getDesktopController];
    [desktopController updateCellBackgroundColor:[sender color]];
}
}

谢谢回复!

1 个答案:

答案 0 :(得分:0)

我将NSWindowDelegate添加到我的控制器并添加方法

- (void)windowWillClose:(NSNotification *)notification {
    if ([notification.object isEqual:[NSColorPanel sharedColorPanel]]) {
        [[NSColorPanel sharedColorPanel] setAction:nil];
    }
}

因此我必须在再次使用之前关闭NSColorPanel。