oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Adding a Preferences Window to Your Application
Pages: 1, 2, 3, 4, 5, 6, 7

We want all six of our check boxes to invoke this method whenever they are changed, so we connect all six check boxes to this single action method of Controller. Drag a wire and make a connection from each check box to Controller. It's a perfectly acceptable practice to have many controls invoke a common action method. When it comes time to coding setColumn, we will use the sender argument that is provided in all action methods to determine which check box sent the message.

Standard operating procedure for applications is that a preferences window will open when the user selects "Preferences..." from the application menu. To enable this we will make a connection between the "Preferences..." menu item and predefined action method of prefsWindow called makeKeyAndOrderFront:. Wiring menu items is just like wiring any other control to an action.

Drag a wire from "Preferences" under "NewApplication" in the menu bar to the preferences window icon, prefsWindow. In the Info window select makeKeyAndOrderFront: in the actions column and click connect (by the way, you can make a connection by double-clicking the action name as well. My roommate just showed me this, and it's quite convenient). Note that depending on the size of your info window, the action name makeKeyAndOrderFront: may be truncated.

Wiring the preferences menu item to the preferences window action makeKeyAndOrderFront:

Finally, we have to wire our "Close" button to some action that closes the window. This is by dragging a wire from the Close button to prefsWindow -- in the list of Actions, choose performClose:. This action simulates a click on the window's close button.

Connecting the close button to the preferences window action performClose:

When you're done setting up the interface, do not create files for Controller, we've already got them and to do so would overwrite all of our previous work. Rather, go back to Project Builder and add the outlets and action declaration to Controller.h manually (we'll fix up Controller.m later on). So in Controller.h add the following six lines to the instance variable declaration block:

    IBOutlet id firstNameCB;
    IBOutlet id lastNameCB;
    IBOutlet id emailCB;
    IBOutlet id homePhoneCB;
    IBOutlet id workPhoneCB;
    IBOutlet id mobilePhoneCB;

And to the method declarations add:

- (void)setColumn:(id)sender;

and that should do it! Now on to the coding.

The coding

To kick off the coding discussion, let's go over what exactly is going to happen with the table column preferences and how they will affect the table view, as well as our strategy for implementing this.

Our strategy

We have seen that the Preferences window consists of six switches that indicate which columns are present in the table, and allows the user to add and remove columns from the table. Six different column choices means that there are two more columns than we initially set up in the table view. We are not going to create a fifth and sixth table column for the table view in Interface Builder, rather, we will do this in code. Everything we do from here on out will be done with code.

At the user-level, the preferences check boxes will function in the following way: When a user selects a previously unselected check box, the indicated table column is immediately added to the table view. When the user selects a check box that is already active, then the indicated column will be removed from the table view.

Let's now discuss the different parts we will need to code to make this work -- our strategy. First, we must have a collection of prebuilt NSTableColumn objects (yes, you can have a table column that is not part of a table and is not shown in the interface) that is created when the application launches.

Second, after the collection of prebuilt objects has been created, the application will look up in the user defaults database a list of columns that the user may have previously selected to include in his table view. If this list exists, the application will reconfigure the table to contain only these columns. If no such list exists, then the application will do nothing and use the table view configuration established in Interface Builder as a default setup.

At this point it will be necessary to set the initial states of the check boxes to match the columns in the tableView, so we'll do that too. Finally, we have to set up code that will add or remove table columns in response to the user changing preferences.

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow