Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [102]

By Root 987 0
is still shown. If you’re using Snow Leopard, you should see an instance of WindowLabAppDelegate already set up in your nib file. Otherwise, drag a plain object from the Library into your main nib window, and change its class to WindowLabAppDelegate using the Identity Inspector. Then Ctrl-drag from the Application icon to the WindowLabAppDelegate instance and connect the application’s delegate outlet. Now open up the empty window contained in your nib, then drag first a wrapping label and then a button from the Library into the empty window. Then lay the window out roughly as shown in Figure 10-3.

Figure 10-3. A very simple window layout

Next, Ctrl-drag from the button to the app delegate in the main nib window, and connect to its showColorPanel: action, and then Ctrl-drag from the app delegate to the label, connecting the title outlet there.

Now save your work, switch back to Xcode, and hit Build & Run. Your new app will appear, and clicking the button will bring up the color panel. Click around on some different colors, and the color of the selected text will immediately change to reflect the new selection.

So, considering that the color panel has no direct connection to our app delegate, it’s fair to wonder: how does that work? How does the changeColor: method in our app delegate get called? The key is the use of the responder chain, as described earlier. NSColorPanel uses the responder chain to find an object that implements the changeColor: method. As the application’s delegate, our little controller object is one of the last objects queried to see if it implements the method, and since it does, it gets called. Note that if the window implemented the method itself, or if it had a delegate that implemented the method, one of those methods would have been called instead.

Now we need to end this section with a reality-check. In reality, what we just did can be more easily (and more handsomely) accomplished by using an NSColorWell, a special control that launches the NSColorPanel when clicked. We’d only need to write code to declare a property in a controller class to contain an NSColor, and then use Cocoa Bindings to bind the NSColorWell’s Value attribute and the NSTextField’s Text Color attribute to the property in our controller. This example is included here, as-is, mainly to show you how to use the color panel from your own code, as well as give you a first look at the responder chain concept.

The Font Panel


The next special panel we’re going to look at is NSFontPanel. Unlike the color panel, the font panel, does not have a matching control that launches it. However, it can be integrated fairly well with the contents of the system’s Format menu, as you’ll see a little later.

What we’re going to do here is create an action method that opens the font panel, and another method which updates the text field. Then we’ll create a button to let the user invoke this functionality. In the WindowLabAppDelegate.h file, add the following method declaration inside the class’s @interface block:

Then switch to the .m file and add the following methods to the @implementation section:

This follows the same usage pattern as the color panel. When the user clicks on a font, the font panel uses the responder chain to look for an object that implements the changeFont: action method, and it manages to find it in our app delegate. Here, things are slightly more complicated, because in both of these methods we make use of a shared instance of the NSFontManager class. A running application’s notion of the selected or current font is held within this shared instance, which we use first in showFontPanel: to indirectly tell the NSFontPanel which font it should begin displaying, and then again in changeFont: to get the new selected font. We get the new font by passing the old font to the font manager’s convertFont: method, which combines characteristics of the old font with the state of the user’s selection in the font panel (for example, if the old font is Lucida Grande/Bold/36, and the user selects Times New Roman,

Return Main Page Previous Page Next Page

®Online Book Reader