Silver Challenge Solution


#1

I used Interface Builder to connect the UISegmentedControl and set the method for when it’s being pressed

WhereamiViewController.h

[code]#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
{
CLLocationManager *locationManager;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
IBOutlet UISegmentedControl *mapVarieties;

}

  • (void)findLocation;
  • (void)foundLocation:(CLLocation *)loc;
  • (IBAction)selectSegmentControl;

@end[/code]

And the implementation of the method for a segment

WhereamiViewController.m

[code]- (IBAction)selectSegmentControl
{

int segmentTouched = [mapVarieties selectedSegmentIndex];
NSString *segmentName = [mapVarieties titleForSegmentAtIndex:segmentTouched];
if ([segmentName isEqualToString:@"Street"]) {
    [worldView setMapType:MKMapTypeStandard];
    
}

if ([segmentName isEqualToString:@"Satellite"]) {
    [worldView setMapType:MKMapTypeSatellite];
}


if ([segmentName isEqualToString:@"Hybrid"]) {
    [worldView setMapType:MKMapTypeHybrid];    
    
}

}
[/code]

Here’s an alternate implementation with a bit less code.

[code]- (IBAction)selectSegmentControl
{
if ([mapVarieties selectedSegmentIndex] == 0) {
[worldView setMapType:MKMapTypeStandard];

}
    
if ([mapVarieties selectedSegmentIndex] == 1) {
    [worldView setMapType:MKMapTypeSatellite];
}
 
    
if ([mapVarieties selectedSegmentIndex] == 2) {
    [worldView setMapType:MKMapTypeHybrid];    
    
}

}[/code]