Tuesday, October 20, 2009

"Lazy" loading of the custom color picker

This is the third in a two-part series :) about a custom color picker. The first post is here and the second is here.

This seemed like a perfect opportunity to learn how to load an accessory window from a second nib. People talk about code reuse, but I haven't done a lot of it. Time to change my ways.

I modified the project slightly before starting. Previously, the App Delegate was the target of the stepper bindings. I moved that code (simply property declarations, and assignment of default values) into a new class called PrefsController, and redid the bindings in IB. After making sure it worked, a copy of the nib file (actually, xib) was renamed as ColorView. I also removed the App Delegate from the object window and removed connections to it from the other objects.

Then I made a new Cocoa Application and added the files to the project. Below is the one line of code needed in the second project's App Delegate. What could be simpler? It seems to work. The window loads and the stepper works properly.

BOOL result = [NSBundle loadNibNamed:@"ColorView" owner:self];

However, at the moment I don't understand something. I guess the objects need to be connected to "File's Owner," but I'm not clear on how to do that. I'll investigate and report back.