Online Book Reader

Home Category

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

By Root 951 0
Save Panel that lets you navigate to a directory of your choice and create a new project there. Use the controls to navigate to a suitable spot in your home directory or your desktop, then enter the name “VillainTracker” and click Save. Now Xcode creates a new folder called VillainTracker in the location you specified, with some default content including the VillainTracker.xcodeproj project file, and opens a new window showing you your project.

Creating the VillainTrackerAppDelegate Class


We’ll start off this project by creating a controller class. For more intricate applications, you’re likely to have several controller classes, but for a simple app like this, which interacts with a single window and the application itself, one controller is all we’ll need. This controller will have outlets to all the objects in our GUI (so that it can set and retrieve values in the view objects any time it needs to), and action methods for those objects to inform the controller every time a user edits a value.

Navigate into your project’s Classes group by first expanding the top-level VillainTracker disclosure triangle if it’s not already open, then doing the same for the Classes group inside of it. If you’re running Snow Leopard, you’ll see that Xcode has created a class called VillainTrackerAppDelegate for you, represented by the two files VillainTrackerAppDelegate.h and VillainTrackerAppDelegate.m. In the version of Xcode available for Leopard, however, the Classes group is empty, so you’ll need to create this class. Right-click (or control-click) on Classes, and in the contextual menu that comes up, navigate into Add and choose New File..., which will bring up the New File wizard. On the left, under Mac OS X, choose Cocoa; and on the right, choose Objective-C class, then click Next. Now you’ll be prompted for the name of your new class file. Enter VillainTrackerAppDelegate.m, make sure the checkbox instructing Xcode to also create a matching .h file is checked, and click Finish. Xcode will create the new files, and bring up an editor window showing you the header file.

Now let’s start paving the way for the model class we’ll be using for this application. In a “real application,” we’d have a proper model class for our villain objects, something that would incorporate saving and loading from a file or a database, and in later chapters you’ll see how to do just that using Core Data. But for this application, where we’re mostly interested in seeing how GUI components are hooked up and used, we’ll keep it simple and use an NSMutableDictionary to represent each of our villain objects. NSMutableDictionary is really quite suitable for this task, because it enables us to easily store and retrieve values of any kind, and doesn’t need any predefined methods for each value, because the values are accessed using simple strings representing keys.

Start by declaring a new instance variable called villain, and declaring it as a property. The header file should now look something like this (the code you’re adding right now is shown here in bold):

The first line we added (between the curly braces) tells the compiler about our new instance variable, while the second line we added (starting with @property) tells the compiler that our class will contain getter and setter methods (called villain and setVillain: respectively) for the villain property, as well as hinting at the semantics of the setter (in particular, retain means that the value passed into setVillain: will be retained).

Now let’s add the getter and setter in the simplest way possible, by using the@synthesize keyword to let the compiler automatically generate them for us. This is done in the class’s implementation, so switch to VillainTrackerAppDelegate.m and add the line seen in bold here:

In case you’ve forgotten, this @synthesize declaration creates a pair of methods for getting and setting the villain property, following the semantics defined in the header. In this case, the @property declaration included retain within parenthesis as part of its declaration, which

Return Main Page Previous Page Next Page

®Online Book Reader