首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSNotificationCenter和mapType

NSNotificationCenter和mapType
EN

Stack Overflow用户
提问于 2012-10-07 15:31:41
回答 2查看 205关注 0票数 0

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

代码语言:javascript
复制
-(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中:

代码语言:javascript
复制
[[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中:

代码语言:javascript
复制
- (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];
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-07 15:52:29

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

票数 1
EN

Stack Overflow用户

发布于 2012-10-07 15:55:13

你错过了开关/中的"break“。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12766954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档