0

I am trying to change the mapType from another ViewController but It only displays the HybridType. Any other mapType is not changing whatever the button on my segmented control is pressed. What am I doing wrong? Thank you in advance..

-(void) receiveTestNotification:(NSNotification *) notification{
_mapView.delegate = self;
if ([[notification name] isEqualToString:@"TestNotification"]) {
    _mapView.mapType = MKMapTypeStandard;
    NSLog(@"Successfully changed maptype");
}
if ([[notification name] isEqualToString:@"TestNotification2"]) {
    _mapView.mapType = MKMapTypeSatellite;
    NSLog(@"Successfully changed maptype");
}
if ([[notification name] isEqualToString:@"TestNotification3"]) {
    _mapView.mapType = MKMapTypeHybrid;
    NSLog(@"Successfully changed maptype");
}

}

in ViewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"TestNotification" object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"TestNotification2" object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"TestNotification3" object:nil];

and in my other viewController:

- (IBAction)segmentedControl:(id)sender {

switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
    case 0:
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self];
        [self dismissModalViewControllerAnimated:YES];
    case 1:
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification2" object:self];
        [self dismissModalViewControllerAnimated:YES];
    case 2:
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification3" object:self];
        [self dismissModalViewControllerAnimated:YES];
}
}
snksnk
  • 1,566
  • 4
  • 21
  • 46

2 Answers2

1

Everythiing seems fine. Check all the iboutlets from the segmented control and log could be more helpful in this case. One other thing you should use break; statement in the switch cases otherwise it goes on executing the code below and so when you want to send TestNotification it will also send other two notifications below.

Sandeep
  • 20,908
  • 7
  • 66
  • 106
1

You miss the "break" in switch / case.

smoothdvd
  • 2,389
  • 4
  • 20
  • 25