objective-ccocoanscolorpanel

NSColorPanel's selector has been called twice


I have shared instance of NSColorPanel and I set selector to him. I use it for background color and for font color. When I want to set text color everything works fine, but when I want to set background color then selector is called twice and background color has been changed to previous color. There is some code:

- (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];
}

Selector:

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

Thx for reply!


Solution

  • I added NSWindowDelegate to my controller and add method

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

    So I have to close NSColorPanel before I use it again.