Online Book Reader

Home Category

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

By Root 837 0
time a user encounters a color well, it may seem confusing. If you click the edge of the color well, the edge becomes highlighted, the Color panel appears, and the well is in active mode.

For the More Curious: NSBundle


A bundle is a directory of resources that may be used by an application. Resources include images, sounds, compiled code, and NIB files. (Users often use the word “plug-in” instead of “bundle.”) The class NSBundle is a very elegant way of dealing with bundles.

Your application is a bundle. In Finder, an application looks to the user like any other file, but it is really a directory filled with NIB files, compiled code, and other resources. We call this directory the main bundle of the application.

Some resources in a bundle can be localized. For example, you could have two versions of foo.nib, one for English speakers and one for French speakers. The bundle would have two subdirectories: English.lproj and French.lproj. You would put an appropriate version of foo.nib in each. When your application asks the bundle to load foo.nib, the bundle will automatically load the French version of foo.nib, if the user has set the preferred language to French. We will cover localization in Chapter 16.

To get the main bundle of an application, use the following code:

NSBundle *myBundle = [NSBundle mainBundle];

This is the most commonly used bundle. If you need to access resources in another directory, however, you could ask for the bundle at a certain path:

NSBundle *goodBundle;

goodBundle = [NSBundle bundleWithPath:@"~/Library/Application Support/MyApp/Good.bundle"];

Once you have an NSBundle object, you can ask it for its resources:

// Extension is optional

NSString *path = [goodBundle pathForImageResource:@"Mom"];

NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];

A bundle may have a library of code. By asking for a class from the bundle, the bundle will link in the library and search for a class by that name:

Class newClass = [goodBundle classNamed:@"Rover"];

id newInstance = [[newClass alloc] init];

If you do not know the name of any classes in the bundle, you can simply ask for the principal class:

Class aClass = [goodBundle principalClass];

id anInstance = [[aClass alloc] init];

As you see, NSBundle is handy in many ways. In this section, the NSBundle was responsible (behind the scenes) for loading the NIB file. If you wanted to load a NIB file without an NSWindowController, you could do it like this:

BOOL successful = [NSBundle loadNibNamed:@"About" owner:someObject];

Note that you would supply the object that will act as the File’s Owner.

Challenge


Create a XIB file with a custom About panel. Add an outlet to AppController to point to the new window. Also add a showAboutPanel: method. Load the NIB by using NSBundle, and make AppController the File’s Owner.

Chapter 13. User Defaults


Many applications have Preferences panels that allow the user to choose a preferred appearance or behavior. The user’s choices go into the user defaults database in the user’s home directory. Note that only the choices that vary from the factory defaults are saved in the user defaults database. If you go to ~/Library/Preferences, you can see your user defaults database. The files are in a binary format, but you can use Xcode’s property list editor to browse though them.

The NSUserDefaults class allows your application to register the factory defaults, save the user’s preferences, and read previously saved user preferences.

The color well that you dropped into the Preferences window in the previous chapter will determine the background color of the table view. When the user changes his or her preference, your application will write the new preference to the user defaults database. When your application creates a new document window, it will read from the user defaults database. As a consequence, only windows created after the change will be affected (Figure 13.1).

Figure 13.1. Completed Application

Also, have you noticed

Return Main Page Previous Page Next Page

®Online Book Reader