Online Book Reader

Home Category

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

By Root 938 0
in place, we’re ready start coding. Before we implement our action methods, you need to learn a bit about NSUserDefaults. As we’ve mentioned, this class lets you store and retrieve a user’s application preferences as a sort of hash or dictionary, keyed off strings of your choosing. One thing that every application should do is create a set of default values for NSUserDefaults, which it will use as a “fallback position” in case the user hasn’t set a value for a given key. For example, say you want to store an integer whose relevant range is a number between one and ten, using the key @“greatness”. Setting a default value of 1 for the @“greatness” key ensures that the first time a user runs the application, and NSUserDefaults tries to retrieve the @“greatness” value, it finds the default value (1) that you specified in code. Without taking this step, retrieving any numeric value that hasn’t already been set by the user will get you a zero, and retrieving any unset object value will get you a nil.

It’s common practice to set up these values in a method that is called early in the application’s startup phase, typically in a class that is included in the main nib. The initialize class method is a good place for this, since it’s called exactly once for each class, the first time a class is accessed. Let’s create such a method in our DungeonThingAppDelegate:

This method calls NSUserDefaults’ registerDefaults: method, passing in a dictionary of default values for our application. We’re setting a default value for every key-name that we’re using in our application, so that we know that when we ask for a value, we’ll always get something relevant.

Create the Action Methods


Now we can start implementing our action methods. Let’s start with createCharacter:, which will display a summary of all the preferences values related to character creation. We start off by grabbing the shared instance of NSUserDefaults, then create an empty string to hold the summary text, and then we actually create the summary, one preferences entry at a time. At the end, we put the summary text into the relevant NSTextField. Note that at the beginning of this method, we create an NSMutableString with a particular capacity, but this is not an upper size limit; NSMutableString is smart enough to “grow” if necessary. The method looks like this:

After entering that code, try compiling and running your application. If all goes well, you should be able to see the main window, hit the Generate Character button, and see a result something like Figure 6-10.

Figure 6-10. The first run of DungeonThing

The next step is to open the Preferences window, and start making some changes in the Character Generation tab. Disable some checkboxes, drag the slider, etc. After each change, click the Generate Character button in the main window again, and the displayed parameters should change to reflect the contents of the Preferences window.

Now that that’s working, let’s fill in the method bodies for createMonster: and createDungeon: as shown below. These methods both work just like the createCharacter: method already shown.

With those methods in place, you should be able to compile and run DungeonThing, modify all the values under every tab in the Preferences window, and see the modified values reflected in the output text fields. The first version of DungeonThing is now complete!

Binding to a Table View


DungeonThing is fine for what it does (aside from the fact that it doesn’t really generate the game objects, of course), but if you started using such a system “in production”

(say, while playing Dungeons and Dragons or a similar game), you’d quickly encounter one major problem: the random game objects aren’t retained in any way! As soon as you click to create a new random character, for example, the previous character is just wiped out, and you have no way of ever seeing it again.

For the next iteration of DungeonThing, we’re going to add some table views to show lists of all created game objects. Clicking on a game object in a table

Return Main Page Previous Page Next Page

®Online Book Reader