ios - NSNotificationCenter 和 mapType

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

最佳答案

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

关于ios - NSNotificationCenter 和 mapType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12766954/

相关文章:

ios - 如何在 swift 中添加位置图标?

iPhone MKMapView 商业应用?

ios - 在顶部插入新行后如何使 UITableViewCell 保持在同一位置?

ios - Swift 3 - iOS 11.2 上的 UICollectionview 问题

iphone - 适用于 iPhone 的 Appdelegate,但不适用于 iPad 版本

ios - 颠倒的方向不会旋转到纵向

ios - 在 ios 中的 imageView 中显示来自路径的图像

ios - 仅在获取用户位置后才拉取 JSON 数据

ios - 呈现时如何避免 View Controller 的多个实例

ios - 如何忽略单个 pod 的 Base SDK