Cocoa Programming for Mac OS X - Aaron Hillegass [71]
Setting the Background Color on the Table View
Open RMDocument.m and import PreferenceController.h at the top. This will allow us to use the BNRTableBgColorKey constant.
#import "PreferenceController.h"
After the NIB file for a new document window has been successfully unarchived, your RMDocument object is sent the message windowControllerDidLoadNib:. At that moment, you can update the background color of the table view.
You should already have this method in RMDocument.m; just edit it to look like this:
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
[tableView setBackgroundColor:
[PreferenceController preferenceTableBgColor]];
}
Build and run your application.
Note that Mac OS X Lion’s state-restoration features may make it tricky to observe the new document preference. You can disable state restoration by editing the Run scheme in Xcode. Open the Product menu and select Edit Scheme. Select the Run RaiseMan.app scheme, change to the Options pane, and check Disable state restoration (Figure 13.5).
Figure 13.5. Disabling State Restoration
For the More Curious: NSUserDefaultsController
Sometimes, you will want to bind to a value from the NSUserDefaults object. An NSUserDefaultsController makes this possible. All the NIBs in your application will use a single shared instance of NSUserDefaultsController.
For example, if you wanted to use bindings (instead of target/action) to deal with the check box on the Preferences panel, you would bind it to the shared NSUserDefaultsController’s value, BNREmptyDocumentFlag (Figure 13.6).
Figure 13.6. Binding to the NSUserDefaultsController
For the More Curious: Reading and Writing Defaults from the Command Line
The user defaults database is found in ~/Library/Preferences/. To edit it from the command line, you use a tool called defaults. For example, to see your defaults for Xcode, you can bring up the Terminal and enter the following command:
defaults read com.apple.Xcode
You should see all your defaults for Xcode. The first few lines of Aaron’s look like this:
{
DocViewerHasSetPrefs = YES;
NSNavBrowserPreferedColumnContentWidth = 155;
NSNavLastCurrentDirectoryForOpen = "~/RaiseMan";
NSNavLastRootDirectoryForOpen = "~";
NSNavPanelExpandedSizeForOpenMode = "{518, 400}";
NSNavPanelFileListModeForOpenMode = 1;
You can also write to the defaults database. To set Xcode’s default directory in the NSOpenPanel to the /Users directory, you could enter this:
defaults write com.apple.Xcode NSNavLastRootDirectoryForOpen /Users
Try this:
defaults read com.bignerdranch.RaiseMan
To see your global defaults, enter this:
defaults read NSGlobalDomain
Challenge
Add to the Preferences panel a button that will remove all the user’s defaults. Label the button Reset Preferences. Don’t forget to update the Preferences window to reflect the new defaults.
Chapter 14. Using Notifications
A user may have several RaiseMan documents open when he or she decides that it is too hard to read them with a purple background. The user opens the Preferences panel and changes the background color but then is disappointed to find that the color of the existing windows doesn’t change. When the user sends you an e-mail about this problem, you reply, “The defaults are read only when the document window is created. Just save the document, close it, and open it again.” In response, the user sends you a mean e-mail. It would be better to update all the existing windows. But how many are there? Will you have to keep a list of all open documents?
What Notifications Are and Are Not
Every running application has an instance of NSNotificationCenter, which functions much like a bulletin board. Objects register as interested in certain notifications (“Please