19
Settings

Many applications include preferences that users can set. Whether users are picking the size of the text or storing usernames, there is a standard way of enabling iOS application preferences. In this chapter, you will use the NSUserDefaults class to add a preference to your Whereami application. This preference will specify the map type of the MKMapView.

Updating Whereami

Every MKMapView has a mapType property that specifies whether it shows roads, satellite imagery, or both. You will allow the user to change this property by adding a UISegmentedControl that toggles the map type. The user’s choice will be saved as a preference for the next time the application is launched.

Open the Whereami project. Then open MainWindow.xib and add a UISegmentedControl to the interface. Change its style and number of segments as shown in Figure 19.1.

Figure 19.1  UISegmentedControl attributes

UISegmentedControl attributes

Then, create an action method and outlet for this control, as shown in Figure 19.2.

Figure 19.2  Adding to Whereami’s interface

Adding to Whereami’s interface

In WhereamiAppDelegate.m, implement this method to change the map type.

-​ ​(​I​B​A​c​t​i​o​n​)​c​h​a​n​g​e​M​a​p​T​y​p​e​:​(​i​d​)​s​e​n​d​e​r​
{​
 ​ ​ ​ ​s​w​i​t​c​h​(​[​s​e​n​d​e​r​ ​s​e​l​e​c​t​e​d​S​e​g​m​e​n​t​I​n​d​e​x​]​)​
 ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​c​a​s​e​ ​0​:​
 ​ ​ ​ ​ ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​w​o​r​l​d​V​i​e​w​ ​s​e​t​M​a​p​T​y​p​e​:​M​K​M​a​p​T​y​p​e​S​t​a​n​d​a​r​d​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​b​r​e​a​k​;​
 ​ ​ ​ ​ ​ ​ ​ ​c​a​s​e​ ​1​:​
 ​ ​ ​ ​ ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​w​o​r​l​d​V​i​e​w​ ​s​e​t​M​a​p​T​y​p​e​:​M​K​M​a​p​T​y​p​e​S​a​t​e​l​l​i​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​b​r​e​a​k​;​
 ​ ​ ​ ​ ​ ​ ​ ​c​a​s​e​ ​2​:​
 ​ ​ ​ ​ ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​w​o​r​l​d​V​i​e​w​ ​s​e​t​M​a​p​T​y​p​e​:​M​K​M​a​p​T​y​p​e​H​y​b​r​i​d​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​b​r​e​a​k​;​
 ​ ​ ​ ​}​
}​

Build and run the application and change the map type to make sure it works. However, if you quit the application (and kill it from the dock), it won’t remember the change you made on the next launch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.122.244