【问题标题】:NSNotificationCenter and mapTypeNSNotificationCenter 和 mapType
【发布时间】:2012-10-07 15:55:13
【问题描述】:

我正在尝试从另一个 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];

在我的另一个视图控制器中:

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

【问题讨论】:

    标签: ios xcode mkmapview uisegmentedcontrol nsnotificationcenter


    【解决方案1】:

    一切似乎都很好。在这种情况下,检查分段控件和日志中的所有 iboutlets 可能会更有帮助。您应该使用 break 的另一件事;在 switch 案例中声明,否则它会继续执行下面的代码,所以当你想发送 TestNotification 时,它也会在下面发送其他两个通知。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多