Online Book Reader

Home Category

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

By Root 1010 0
controller object to deal with, and which string it should use as a key for getting and setting a value. You can access model objects through your own controller classes, or using generic controller classes provided by Apple.

This results in an architecture where your own controller class often doesn’t need to know anything specific about any of the view objects in use. You don’t need to have instance variables pointing at them, and you don’t need to implement action methods to get their input! Your controller ends up as little more than a simple channel between a model object and a bunch of view objects, and from the controller’s point of view it doesn’t make a difference if there are ten views or a hundred.

In this chapter, you’ll learn how to use Cocoa Bindings with simple controls such as checkboxes, sliders, and text fields, as well as with complex controls such as table views. You’ll create bindings that connect to model objects through a couple of Apple’s included controller classes. You’ll also get an introduction to using NSUserDefaults and its bindings-ready friend NSUserDefaultsController to deal with preferences in your application.

Binding to Simple Controls


The example application for this chapter is something that could be used by a Game Master in a role-playing game to randomly create characters, monsters, and dungeons. The main window will contain buttons that create random game objects when the user clicks them, and text fields to display the result. We’ll also make a Preferences window where the user can specify some parameters for the creation of the game objects. We won’t do the actual “rolling” of these random objects. We’ll just display a summary of the parameters used each time the user clicks one of the buttons. The random game object creation is a bit too far afield for this book, and could be a fun “exercise for the reader” if you are so inclined!

Create the DungeonThing Project


Launch Xcode, and select File➤New Project from the menu. Choose Cocoa Application, then navigate to the directory where you want to create your new project, and name it DungeonThing. Now create a DungeonThingAppDelegate class, as you’ve done in the earlier projects (if you’re running Snow Leopard, it’s already created for you). The DungeonThingAppDelegate class interface is pretty simple; we’re going to create quite a lot of options that the user can select in the Preferences window, but our controller doesn’t need to know about any of those GUI controls, because they’ll all store their values directly into the app’s preferences via NSUserDefaultsController, one of Cocoa’s bindings-ready controller classes. All we need to add are three action methods, one for each “create” button to call when it’s clicked, as well as outlets for three text fields in the GUI, where we’ll display the results. In the second half of this chapter, even those three outlets will go away, because those text fields will also be handled using Cocoa Bindings. For now, make your DungeonThingAppDelegate.h look like this (new parts are in bold):

Then switch over to DungeonThingAppDelegate.m, and add the code shown in bold. As before, we provide empty method definitions for each action, for the sake of having code that actually compiles.

After entering this code, compile your application just to make sure there are no syntax errors. We’ll come back and fill in the code for those action methods later.

Create a Preferences Window


Now find the MainMenu.xib in Xcode (you’ll find it in Resources in the navigation pane) and double-click to open it in Interface Builder. This starts off as a standard empty application GUI which you should be familiar with by now. As before, you’ll probably want to edit some items in the menu, changing every occurrence of “NewApplication” to “DungeonThing.”

This nib file already contains a main window, which we’ll get to a little later on, but first we’ll get to the heart of the matter by creating a Preferences window, whose GUI controls will be configured exclusively with Cocoa Bindings.

Return Main Page Previous Page Next Page

®Online Book Reader