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

406

CHAPTER 11: iPad Considerations

self.languagePopoverController = nil;

}

}

Now, let’s define what will happen when the user taps the Choose Language button. Simply put, we create a BIDLanguageListController, wrap it in a UIPopoverController, and display it. Place this method at the bottom of the file, just before the @end:

- (IBAction)touchLanguageButton {

if (self.languagePopoverController == nil) { BIDLanguageListController *languageListController =

[[BIDLanguageListController alloc] init]; languageListController.detailViewController = self; UIPopoverController *poc = [[UIPopoverController alloc]

initWithContentViewController:languageListController]; [poc presentPopoverFromBarButtonItem:languageButton

permittedArrowDirections:UIPopoverArrowDirectionAny

animated:YES]; self.languagePopoverController = poc;

} else {

if (languagePopoverController != nil) { [languagePopoverController dismissPopoverAnimated:YES]; self.languagePopoverController = nil;

}

}

}

The final change is to add these lines to the viewDidUnload method:

self.languageButton = nil; self.languagePopoverController = nil;

And that’s all! You should now be able to run the app in all its glory, switching willy-nilly between presidents and languages. Switching from one language to another should always leave the chosen president intact, and likewise switching from one president to another should leave the language intact.

iPad Wrap-Up

In this chapter, you learned about the main GUI components that are available only on the iPad: popovers and split views. You’ve also seen an example of how a complex iPad application with several interconnected view controllers can be configured entirely within Interface Builder. With this hard-won knowledge, you should be well on your way to building your first great iPad app. If you want to dig even further into the particulars of iPad development, you may want to take a look at Beginning iPad Development for iPhone Developers by David Mark, Jack Nutting, and Dave Wooldridge (Apress, 2010).

Next up, it’s time to visit application settings and user defaults.

www.it-ebooks.info

Chapter12

Application Settings and

User Defaults

All but the simplest computer programs today have a preferences window where the user can set application-specific options. On Mac OS X, the Preferences… menu item is usually found in the application menu. Selecting it brings up a window where the user can enter and change various options. The iPhone and other iOS devices have a dedicated application called Settings, which you no doubt have played with any number of times. In this chapter, we’ll show you how to add settings for your application to the Settings application, and how to access those settings from within your application.

Getting to Know Your Settings Bundle

The Settings application lets the user enter and change preferences for any application that has a settings bundle. A settings bundle is a group of files built in to an application that tells the Settings application which preferences the application wishes to collect from the user.

Pick up your iOS device, and locate your Settings icon. By default, you’ll find it on the home screen (see Figure 12–1).

D.Mark et al., Beginning iOS 5 Development

©Dave Mark, Jack Nutting, Jeff LaMarche 2011

www.it-ebooks.info

408

CHAPTER 12: Application Settings and User Defaults

Figure 12–1. The Settings application icon is in the middle of the last column on this iPhone. It may be in a different spot on your device, but it’s always available.

When you touch the icon, the Settings application will launch. Ours is shown in Figure 12–2.

www.it-ebooks.info

CHAPTER 12: Application Settings and User Defaults

409

Figure 12–2. The Settings application

The Settings application acts as a common user interface for the iOS User Defaults mechanism. User Defaults is the part of the system that stores and retrieves preferences.

In an iOS application, User Defaults is implemented by the NSUserDefaults class. If you’ve done Cocoa programming on the Mac, you’re probably already familiar with NSUserDefaults, because it is the same class that is used to store and read preferences on the Mac. Your applications will use NSUserDefaults to read and store preference data using a key value, just as you would access keyed data from an NSDictionary. The difference is that NSUserDefaults data is persisted to the file system, rather than stored in an object instance in memory.

In this chapter, we’re going to create an application, add and configure a settings bundle, and then access and edit those preferences from within our application.

One nice thing about the Settings application is that it provides a solution so that you don’t need to design your own user interface for your preferences. You create a property list defining your application’s available settings, and the Settings application creates the interface for you.

Immersive applications, such as games, generally should provide their own preferences view so that the user doesn’t need to quit in order to make a change. Even utility and

www.it-ebooks.info

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