Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [66]

By Root 895 0
running for a while, the objects that already exist need to establish some connection to the objects read from the NIB file. File’s Owner provides this connection. File’s Owner is a placeholder in a NIB file for an object that will already exist when the NIB file is loaded. An object loading a NIB file will provide the owner object. The owner is put into the place that File’s Owner represents. In your application, the owner will be the instance of PreferenceController that was created by the AppController.

The use of File’s Owner is confusing to many people. You will not instantiate PreferenceController in the NIB file. Instead, you have just informed the NIB file that the owner (which will be provided when the NIB file is loaded) is a PreferenceController.

Lay Out the User Interface

With Preferences.xib open in the editor, create a new panel by dragging a panel from the Library (under Application->Windows) and dropping it anywhere onto the screen (Figure 12.7).

Figure 12.7. Create an Instance of NSPanel

Make the panel smaller and drop a color well and a check box onto it. Label them as shown in Figure 12.8. (Check boxes have labels, but you will have to drag out a label for the color well.)

Figure 12.8. Completed Interface

Set the target of the color well to be File’s Owner (your PreferenceController) and set the action to be changeBackgroundColor: (Figure 12.9).

Figure 12.9. Set the Target of the Color Well

Also, make your PreferenceController be the target of the check box and set the action to be changeNewEmptyDoc:.

Control-click on File’s Owner to bring up the connections window. Set the colorWell outlet of File’s Owner to the color well object. Set the checkbox outlet of File’s Owner to the check box object. See Figure 12.10.

Figure 12.10. Set the colorWell and checkbox Outlets

Control-click File’s Owner to get the connection window. Connect the window outlet to the panel (Figure 12.11).

Figure 12.11. Set the window Outlet of File’s Owner

Open the Attributes Inspector for the panel. Disable resizing. Change the title on the window to Preferences (Figure 12.12).

Figure 12.12. The New Window’s Attributes

PreferenceController.m


In Xcode, edit PreferenceController.m to look like this:

#import "PreferenceController.h"

@implementation PreferenceController

- (id)init

{

self = [super initWithWindowNibName:@"Preferences"];

return self;

}

- (void)windowDidLoad

{

[super windowDidLoad];

NSLog(@"Nib file is loaded");

}

- (IBAction)changeBackgroundColor:(id)sender

{

NSColor *color = [colorWell color];

NSLog(@"Color changed: %@", color);

}

- (IBAction)changeNewEmptyDoc:(id)sender

{

NSInteger state = [checkbox state];

NSLog(@"Checkbox changed %ld", state);

}

@end

Note that you set the name of the NIB file to be loaded in the init method. This NIB file will be loaded automatically when it is needed. The instance of PreferenceController will be substituted for the File’s Owner in the NIB file.

After the NIB file is loaded, the PreferenceController will be sent windowDidLoad. It offers an opportunity (similar to awakeFromNib or windowControllerDidLoadNib:) for the controller object to initialize the user interface objects that have been read from the NIB file.

When sent showWindow: for the first time, the NSWindowController automatically loads the NIB file and moves the window onto the screen and to the front. The NIB file is loaded only once. When the user closes the Preferences panel, it is moved off screen but not deallocated. The next time the user asks for the Preferences panel, it is simply moved onto the screen.

The changeBackgroundColor: and checkboxChanged: methods are quite boring right now—they simply print out a message. In the next chapter, you will change them to update the user’s defaults database.

Build and run the application. The new panel should appear, and altering the check box or color well should result in a message in the console (Figure 12.13).

Figure 12.13. Completed Application

The first

Return Main Page Previous Page Next Page

®Online Book Reader