In-app "Visible at launch" checkbox?

Discussion in 'Mac Programming' started by macindev, Aug 12, 2009.

  1. macindev macrumors newbie

    Jul 6, 2009
    Seattle, Washington, USA
    I'm trying to figure out how to make a checkbox on a window so the user could select wether or not they want to see the window again. The code would maybe look something like this?
    - ([COLOR="magenta"]BOOL[/COLOR])windowIsVisibleAtLaunch:([COLOR="magenta"]id[/COLOR])sender{
        [[COLOR="teal"]welcomeWindow[/COLOR] ([COLOR="indigo"]method[/COLOR]):[[COLOR="teal"]welcomeCheckBox[/COLOR] [COLOR="indigo"]booleanValue[/COLOR]]];
    The "(method)" is where the missing method needs to go.
    Any help would be much appreciated.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    I'd do it like this: wrap your window into an NSWindowController, bind the checkbox to NSUserDefaultsController, and then at startup check to see if that user defaults key exists, and if it doesn't, show the window.
  3. macindev thread starter macrumors newbie

    Jul 6, 2009
    Seattle, Washington, USA
    "Visible at launch" checkbox

    So I did what you said. After 40 lines of code and countless error encounters I have gotten close. Unfortunately, the API guide doesn't give a very good explanation of NSUserDefaults and using them was a disaster. So, I looked at an example for a preferences controller. I used some code to get me by and I think I might be close. Here's the code I have so far:
    #define setBoolDefault(name) \
    {if ([[defaultValues() objectForKey:name] isEqual:[dict objectForKey:name]]) [defaults removeObjectForKey:name]; else [defaults setBool:[[dict objectForKey:name] boolValue] forKey:name];}
    + (void)savePreferencesToDefaults:(NSDictionary *)dict {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    - (void)miscChanged:(id)sender {
        static NSNumber *yes = nil;
        static NSNumber *no = nil;
        int anInt;
        if (!yes) {
            yes = [[NSNumber alloc] initWithBool:YES];
            no = [[NSNumber alloc] initWithBool:NO];
        [displayedValues setObject:[welcomeCheckBox state] ? yes : no forKey:CheckBox];	
    	[self commitDisplayedValues];
    - (void)showIfChecked:(id)sender{
    	if (welcomeCheckBox boolValue:YES)
    [COLOR="red"]Unfortunatley, here I get a syntax error before the "boolValue"...[/COLOR]
    		[welcomeWindow makeKeyAndOrderFront:nil];
    	else {
    		[mainAppWindow makeKeyAndOrderFront:nil];
    I am a beginner coder and it would be helpful to get a detailed answer.
    Also, if you know of a way to shorten the code PLEASE TELL because I think I'm going to have carpal tunnel by the time I finish this app!

Share This Page