Notes on 'silver' challenge


#1

I found this quite time-consuming and there wasn’t much on the board to help me out, so here are some notes if you get stuck.

I added a new property to the controller (.h):

IBOutlet UISegmentedControl *mapType;

And a new action function:

- (IBAction)changeMapType:(id)sender;

Then I added a segmented control to the interface (.xib file), configured it to have 3 segments, and made the following connections:

  • From the mapType object to the UI component
  • From the valueChanged event on the UI component to the File’s Owner -> changeMapType (This was the most challenging part because I couldn’t see a ‘delegate’ to connect)

After that, it was just a matter of implementing the new function:

[code]- (IBAction)changeMapType:(id)sender {
NSLog(@“Button Push! %d”, (int)[mapType selectedSegmentIndex]);
switch ([mapType selectedSegmentIndex]) {
case 0:
[worldView setMapType:MKMapTypeStandard];

        break;
        
    case 1:
        [worldView setMapType:MKMapTypeHybrid];
        
        break;
        
    case 2:
        [worldView setMapType:MKMapTypeSatellite];
        
        break;
        
    default:
        break;
}

}[/code]

If I’ve made this more complicated than it needed to me, please let me know!


#2

Yours looks fine. I did all the configurations from code.

This is how it looks like (of course you need to connect the IBOutlets and view objects on the xib):

[code]-(void) viewDidLoad
{
// This will find and show user’s location on map
[worldView setShowsUserLocation:YES];

// Perform UISegmentedControl setup
[mapChoices removeAllSegments];

[mapChoices insertSegmentWithTitle:@"Standard" atIndex:0 animated:YES];
[mapChoices insertSegmentWithTitle:@"Satellite" atIndex:1 animated:YES];
[mapChoices insertSegmentWithTitle:@"Hybrid" atIndex:2 animated:YES];

mapChoices.selectedSegmentIndex = 0;

// Add target action for UISegmentedControl
[mapChoices  addTarget:self
             action:@selector(pickOne:)
             forControlEvents:UIControlEventValueChanged];

}

// Action for the UISegementedControl
-(void) pickOne:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;

worldView.mapType = [segmentedControl selectedSegmentIndex];

}[/code]