NSNotificationCenter和mapType

时间:2012-10-07 07:31:41

标签: ios xcode mkmapview uisegmentedcontrol nsnotificationcenter

我正在尝试从另一个ViewController更改mapType,但它只显示HybridType。无论按下我的分段控件上的按钮,任何其他mapType都不会更改。我究竟做错了什么?提前谢谢..

-(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");
}

}
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];

和我的另一个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];
}
}

2 个答案:

答案 0 :(得分:1)

Everythiing似乎很好。在这种情况下,检查分段控件和日志中的所有iboutlet可能会更有帮助。还有一件事你应该使用休息;在交换机情况下的语句,否则它继续执行下面的代码,所以当你想发送TestNotification时,它也会发送下面的其他两个通知。

答案 1 :(得分:1)

你错过了switch / case中的“中断”。