Online Book Reader

Home Category

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

By Root 928 0
its current value to 10. A bit further down, click to turn on the Continuous checkbox (so the slider will report its value continuously while the user is dragging it), and you’re all set. Figure 6-3 shows you how it’s done.

Figure 6-3. Configuration of an NSSlider for selecting an integer between 2 and 20

One other adjustment that needs to be made, but can’t be seen from the screenshot, is to make the matrix containing the checkboxes behave appropriately in response to mouse clicks. Because NSMatrix is designed to contain a variety of controls, it has multiple ways of interacting with its cells when the user clicks on one of them. Select the matrix of checkboxes, and in the Attributes Inspector, set the Mode popup to Highlight. This mode makes the matrix handle a click in a cell by toggling the clicked cell’s selected state between zero and one, which switches the checkbox on and off.

The last configuration required for this tab is to set the tags for the two radio buttons, so we can tell them apart later (the checkboxes don’t need to have their tags set, because they will be dealt with a little differently when we get to the bindings). Do this by keeping the Attributes Inspector up, then click on first one radio button and then the other, typing in new tag values for each of them in the Inspector. Set the tag for the first radio button cell to 1, and set the second to 2.

Monster Generation Preferences


The next tab contains controls that let the user specify preferences for random monster generation, which you can see depicted in Figure 6-4.

Figure 6-4. Monster Generation preferences

Here we have a slider, a matrix of checkboxes, and a couple of text fields as labels. The slider should be configured similarly to the one used in the Character Generation tab, but with a few different attributes: minimum value of 1, maximum value of 10, and showing 10 tick marks.

Dungeon Generation Preferences


Finally, we’ll create the GUI for the Dungeon Generation preferences. This is a simple one, with just sliders and text fields, as seen in Figure 6-5.

Figure 6-5. Dungeon Generation preferences tab

These sliders should be configured just like the one in the Monster Generation tab, with a range from 1 to 10, and showing 10 ticks.

Binding to NSUserDefaultsController


At this point, we have all these GUI controls in a window, but we have no outlets to connect to them, and no action methods for them to call when the user clicks on them. So, now what? Now it’s time to create your first bindings! We’re going to use a class called NSUserDefaultsController, which is a bindings-ready generic controller class that is included in Cocoa. A bindings-ready controller like this lets us bind view objects to an underlying model object right within Interface Builder. One nice thing about this class is that it maintains its own storage in the form of NSUserDefaults, which is the standard class used in Cocoa applications for saving and retrieving a user’s application preferences. This will let us bind each view object’s value to a uniquely keyed value in the application’s preferences. These preferences are automatically saved before a user exits the application, and reloaded the next time the user launches the application again.

Bindings for Character Generation


Go to the Preferences window we’re building, and switch back to the Character Generation pane. Click on the slider, then bring up the Bindings Inspector by pressing ⌘4. In this Inspector, you can see all the attributes of a view object that can be bound to a value in a model object. Most often, you’ll be binding the Value to something, but each view class offers its own set of attributes available for binding. The slider, for instance, can have its Max Value and Min Value attributes bound to something, which would allow you to vary those extremities based on values in a model object. Some other view objects can bind text colors and fonts to model values, and most of them can have their Hidden and Enabled states bound to model values.

Return Main Page Previous Page Next Page

®Online Book Reader