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.
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.
Then, create an action method and outlet for this control, as shown in Figure 19.2.
In WhereamiAppDelegate.m, implement this method to change the map type.
- (IBAction)changeMapType:(id)sender { switch([sender selectedSegmentIndex]) { case 0: { [worldView setMapType:MKMapTypeStandard]; }break; case 1: { [worldView setMapType:MKMapTypeSatellite]; }break; case 2: { [worldView setMapType:MKMapTypeHybrid]; }break; } }
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.
18.118.122.244