macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Adding a New Style Preferences Window to Your App, Part 2
Pages: 1, 2, 3

Your Second Preference Pane

Repeat the above steps exactly, but substitute "Advanced" for "General" throughout. That is, your PreferencePanes subdirectory, Group, and the new target will be called "AdvancedPrefs," with AdvancedPreferences.tiff (again from Safari) as the icon, with AdvancedPrefs-Info.plist, an AdvancedPreferencePaneController class, and AdvancedPrefs.nib file.



Take care not to miss any steps, but hopefully it will be a bit quicker the second time around.

Build and run the project, and your preferences window should look like the one shown in Figure 2.

figure

Figure 2. Two preference panes

If you switch between preference panes, and then quit the application, note that the last selected preference pane is shown after the restart. SS_PrefsController handles this for you.

Adding Controls to Your Preferences Window

We now have a groovy new-style preferences window, but to make it useful we need to add some controls, so that we can read and set the values of our actual preferences.

General Preferences

We will represent our preferenceOne (a Boolean) value using a pair of radio buttons on the General preferences pane.

First, we will add some convenience methods to GeneralPreferencePaneController:

- (int) preferenceOneSelectedIndex{
    return [_controller preferenceOne] ? 0 : 1;
}

- (void) setPreferenceOneSelectedIndex:(int) selectedIndex{
    [_controller setPreferenceOne: (selectedIndex == 0)];
}

Remember to add the method prototypes to GeneralPreferencePaneController.h as well. You can also import NPEController.h to GeneralPreferencePaneController, to prevent some compiler warnings about the preferenceOne methods.

Then, in Interface Builder, open GeneralPrefs.nib, and drag a pair of radio buttons and a label from the palette into the custom view, and arrange them as shown Figure 3 below.

Open the Inspector for the NSMatrix object that contains the radio buttons, and bind selectedIndex to File's Owner, with Model Key Path set to preferenceOneSelectedIndex.

Save the nib file, and build and run your project again in Xcode.

The General Preference pane should now contain the controls that you added. If you change the value, and then restart your application, you will see that, as one would hope, the preferenceOne value is now set to the new value.

figure

Figure 3. The general preference pane, with controls

Advanced Preferences

We will represent our preferenceTwo value (an int) in a drop-down menu.

To support this, add the following methods to AdvancedPreferencePaneController.m (and add the prototypes and an import of NPEController.h to AdvancedPreferencePaneController.h).

- (NSArray *) preferenceTwoLabelArray{
    return [NSArray arrayWithObjects: @"Zero", @"One", @"Two", @"Three", nil];
}

- (int) preferenceTwo{ return [_controller preferenceTwo]; }

- (void) setPreferenceTwo:(int) preferenceTwo{
    [_controller setPreferenceTwo:preferenceTwo];
    if(preferenceTwo > 2){
        NSDictionary *errorDict = 
            [NSDictionary dictionaryWithObjectsAndKeys:
                @"preferenceTwo value alert", NSLocalizedDescriptionKey,
                @"preferenceTwo value is set to a high value.", NSLocalizedRecoverySuggestionErrorKey,
                nil];

        NSError *error = [NSError errorWithDomain:@"privateDomain" code:0 userInfo:errorDict];
        [self showWarningAlert:error];
    }
}

Then, in Interface Builder, open AdvancedPrefs.nib, and drag an NSPopupButton and a corresponding label from the palette, and arrange them as shown in Figure 4 below. You do not need to touch the menu items in the NSPopupButton--just leave the default ones in place.

Also, drag an Array Controller from the palette to the Instances pane of the AdvancedPrefs window in IB. Bind the content array of the array controller to File's Owner, with Model Key Path set to "preferenceTwoLabelArray."

figure

Figure 4. The advanced preference pane, with controls

Then add the following bindings to the NSPopupButton that you just added: bind both content and content values to the NSArrayController, with Controller Key arrangedObjects. Bind selectedIndex to File's Owner, with model key path preferenceTwo.

Save the nib file, and build and run your project again in Xcode.

The Advanced Preference pane should now contain the controls that you added. Again, changes to the value should persist between restarts of the application, just like a proper preference pane should.

If you set the value of preferenceTwo to be Three, you should receive the warning alert that was defined in the setPreferenceTwo method of AdvancedPreferencePaneController.

Pages: 1, 2, 3

Next Pagearrow