Life as Clay

Cocoa: How to close the system color picker panel

with 3 comments


I have a color well in the preferences sheet of the app that I’m building. When I selected a color and then closed the preferences sheet, the system color panel did not automatically close. I googled this and didn’t find an answer immediately, so I searched through the docs. This is the method that I use to close the preferences sheet now:

-(IBAction) hidePreferencesSheet:(id)sender {
	[NSApp endSheet:preferencesSheet];

	if ([NSColorPanel sharedColorPanelExists]) {
		[[NSColorPanel sharedColorPanel] close];
	}

	[preferencesSheet orderOut:sender];
}

Basically, NSColorPanel is the class that you’re dealing with. The class method +(BOOL)sharedColorPanelExists tells you whether the panel is open. NSColorPanel is a subclass of NSWindow. I use [NSColorPanel sharedColorPanel] to return the system color panel and then the NSWindow close method to close it. Pretty simple!

Advertisements

Written by Clay

January 22, 2011 at 10:10

Posted in Cocoa, Objective-C

3 Responses

Subscribe to comments with RSS.

  1. I googled this and didn’t find an answer immediately, so I searched through the docs.When I selected a color and then closed the preferences sheet, the system color panel did not automatically close.?

    programmersclub ru

    January 29, 2011 at 19:39

  2. I would have to see the context of what you’re doing to help.

    Clay

    January 31, 2011 at 17:05

  3. This worked perfectly for me… although I included it in a place other than the suggested ‘hidePreferenceSheet’ location as shown. Y’d THINK they’d have included this clue in the NSColorPanel_Class docs – but it’s not there because it’s an inherited behavior from the NSWindow class.

    Lance Drake

    October 4, 2012 at 12:21


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: