【问题标题】:Annotation callout view is not responding注释标注视图没有响应
【发布时间】:2014-03-17 02:59:49
【问题描述】:

我正在尝试在 mapView 上显示注释。所有注释都来自 JSON 对象。他们分为三组。用户可以通过选择 segmentedIndex 控件上的选项来选择应显示哪些注释。

就目前而言,应用程序按预期运行,用户从 segmentedIndex 控件中选择一个选项,并且注释显示在 mapView 上。

我当前的问题是我需要用户单击标注视图以打开另一个视图控制器。

我认为我的代码是正确的,但我想它不是这样显示的标注视图是默认标注视图,带有标题和副标题。单击它时不会触发任何操作。

欢迎任何帮助。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[PlaceMark class]]) {

        MKPinAnnotationView *annotationView =
        (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc]
                              initWithAnnotation:annotation
                              reuseIdentifier:identifier];
        } else {
            annotationView.annotation = annotation;
        }

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;

        // Create a UIButton object to add on the
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [annotationView setRightCalloutAccessoryView:rightButton];

        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
        [leftButton setTitle:annotation.title forState:UIControlStateNormal];
        [annotationView setLeftCalloutAccessoryView:leftButton];

        return annotationView;
    }

    return nil; 
}
- (void)mapView:(MKMapView *)mapView
 annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
        // Do your thing when the detailDisclosureButton is touched
        UIViewController *mapDetailViewController = [[UIViewController alloc] init];
        [[self navigationController] pushViewController:mapDetailViewController animated:YES];

    } else if([(UIButton*)control buttonType] == UIButtonTypeInfoDark) {
        // Do your thing when the infoDarkButton is touched

        NSLog(@"infoDarkButton for longitude: %f and latitude: %f",
              [(PlaceMark*)[view annotation] coordinate].longitude,
              [(PlaceMark*)[view annotation] coordinate].latitude);
    }
}

【问题讨论】:

  • 很可能地图视图的delegate 没有设置,在这种情况下它不会调用viewForAnnotation。如果设置了委托,请在设置辅助按钮的行上方或下方放置断点或 NSLog,并确保代码正在执行。
  • 不相关但是:在calloutAccessoryControlTapped 中,您可能只想知道它是 right 还是 ,而不是检查 buttonType按钮,所以只需执行if (control == view.rightCalloutAccessoryView)等。请参阅stackoverflow.com/a/9113611/467105以获取示例。
  • @Anna,我在头文件中有以下声明: interface mapKitViewController : UIViewController ,你的意思是我已经设置了地图视图的委托吗?或者你的意思是我应该把它放在另一个地方?
  • 头文件中的声明没有设置地图视图的委托。这只是告诉编译器这个类打算实现某些委托方法。在 xib/storyboard 中,右键单击地图视图并将代理连接到视图控制器或在 viewDidLoad 中输入mapView.delegate = self;
  • 谢谢@Anna,我已经更改了我所有的 viewController 代码以使其更清晰。如果你不介意,我希望你把你的 cmets 放在一个答案中,让我接受它。

标签: ios mapkit mkannotationview


【解决方案1】:

很可能地图视图的delegate 没有设置,在这种情况下它不会调用viewForAnnotation,而是会创建一个默认视图(带有标注的红色图钉仅显示标题和副标题——没有按钮)。

头文件中的声明没有设置地图视图的delegate。这只是告诉编译器这个类打算实现某些委托方法。

在 xib/storyboard 中,右键单击地图视图并将delegate 出口连接到视图控制器,或者在viewDidLoad 中输入mapView.delegate = self;


不相关,但我想指出,在calloutAccessoryControlTapped 中,您可能只想知道它是 right 还是 leftbuttonType /em> 按钮,就这样吧:

if (control == view.rightCalloutAccessoryView) ... 

完整示例请参见https://stackoverflow.com/a/9113611/467105

检查buttonType至少有两个问题:

  • 如果您想对两个按钮使用相同的类型(例如自定义)怎么办?
  • 在 iOS 7 中,将按钮设置为 UIButtonTypeDetailDisclosure 最终会实际创建一个 Info 类型的按钮(有关详细信息,请参阅 MKAnnotationView always shows infoButton instead of detailDisclosure btn)。因此,对 buttonType UIButtonTypeDetailDisclosure 的检查将失败(在 iOS 7 上)。

【讨论】:

  • 感谢...您的时间、精力和耐心。
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
相关资源
最近更新 更多