Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

426

CHAPTER 12: Application Settings and User Defaults

Figure 12–23. Three fields down. Not too shabby!

Adding a Toggle Switch Setting

The next item we need to get from the user is a Boolean value that indicates whether our warp engines are turned on. To capture a Boolean value in our preferences, we are going to tell the Settings application to use a UISwitch by adding another item to our

PreferenceSpecifiers array with a type of PSToggleSwitchSpecifier.

Collapse Item 3 if it’s currently expanded, and then single-click it to select it. Press return to create Item 4. Use the drop-down menu to select Toggle Switch, and then click the disclosure triangle to expand Item 4. You’ll see there’s already a child row with a Key of Type and a Value of PSToggleSwitchSpecifier. Give the empty Title row a value of

Warp Drive, and set the value of the Key row to warp.

We have one more required item in this dictionary, which is the default value. Just as with the Multi Value setup, here Xcode has already created a DefaultValue row for us. Let’s turn on our warp engines by default by giving the DefaultValue row a value of YES. Figure 12–24 shows our completed Item 4.

www.it-ebooks.info

CHAPTER 12: Application Settings and User Defaults

427

Figure 12–24. Our finished Item 4, a toggle switch to turn the warp engines on and off. Engage!

Adding the Slider Setting

The next item we need to implement is a slider. In the Settings application, a slider can have a small image at each end, but it can’t have a label. Let’s put the slider in its own group with a header so that the user will know what the slider does.

Start by collapsing Item 4. Now, single-click Item 4 and press return to create a new row. Use the popup to turn the new item into a Group, and then click the item’s disclosure triangle to expand it. You’ll see that Type is already set to PSGroupSpecifier. This will tell the Settings application to start a new group at this location. Double-click the value in the row labeled Title, and change the value to Warp Factor.

Collapse Item 5 and select it, and then press return to add a new sibling row. Use the popup to change the new item into a Slider, which indicates to the Settings application that it should use a UISlider to get this information from the user. Expand Item 6 and set the value of the Key row to warpFactor so that the Settings application knows which key to use when storing this value.

We’re going to allow the user to enter a value from 1 to 10, and we’ll set the default to warp 5. Sliders need to have a minimum value, a maximum value, and a starting (or default) value, and all of these need to be stored as numbers, not strings, in your property list. Fortunately, Xcode has already created rows for all these values. Give the DefaultValue row a value of 5, the MinimumValue row a value of 1, and the

MaximumValue row a value of 10.

If you want to test the slider, go ahead, but hurry back. We’re going to do just a bit more customization.

As noted, sliders can have images. You can place a small 21 × 21-pixel image at each end of the slider. Let’s provide little icons to indicate that moving the slider to the left slows us down, and moving it to the right speeds us up.

Adding Icons to the Settings Bundle

In the 12 - AppSettings folder in the project archive that accompanies this book, you’ll find two icons called rabbit.png and turtle.png. We need to add both of these to our settings bundle. Because these images need to be used by the Settings application, we

www.it-ebooks.info

428

CHAPTER 12: Application Settings and User Defaults

can’t just put them in our AppSettings folder; we need to put them in the settings bundle so the Settings application can access them.

To do that, find the Settings.bundle in the project navigator. We’ll need to open this bundle in the Finder. Control-click the Setting.bundle icon in the project navigator. When the contextual menu appears, select Show in Finder (see Figure 12–25) to show the bundle in the Finder.

Figure 12–25. The Settings.bundle contextual menu

Remember that bundles look like files in the Finder, but they are really folders. When the Finder window opens to show the Settings.bundle file, control-click the file and select Show Package Contents from the contextual menu that appears. This will open the settings bundle in a new Finder window, and you should see the same two items that you see in Settings.bundle in Xcode. Copy the two icon files, rabbit.png and turtle.png, from the 12 - AppSettings folder into the Settings.bundle package contents Finder window.

You can leave this window open in the Finder, as we’ll need to copy another file here soon. Now, we’ll return to Xcode and tell the slider to use these two images.

Back in Xcode, return to Root.plist and add two more child rows under Item 6. Give one a key of MinimumValueImage and a value of turtle.png. Give the other a key of

MaximumValueImage and a value of rabbit.png. Our finished Item 6 is shown in Figure 12–26.

www.it-ebooks.info

CHAPTER 12: Application Settings and User Defaults

429

Figure 12–26. Our finished Item 6, a slider with turtle and rabbit icons to represent slow and fast

Save your property list, and let’s build and run our app to make sure everything is still hunky-dory. You should be able to navigate to the Settings application and find the slider waiting for you, with the sleepy turtle and the happy rabbit at each end (see Figure 12–27).

Figure 12–27. We have text fields, multivalue fields, a toggle switch, and a slider. We’re almost finished.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]