注释标注视图没有响应

时间:2014-03-17 02:59:50

标签: ios mapkit mkannotationview

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

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

我目前的问题是我需要用户点击标注视图以打开另一个viewController。

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

欢迎任何帮助。

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

1 个答案:

答案 0 :(得分:1)

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

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

在xib / storyboard中,右键单击地图视图并将delegate插座连接到视图控制器,或者在viewDidLoad中放置mapView.delegate = self;


不相关,但我想在calloutAccessoryControlTapped中指出,而不是检查 buttonType ,您可能只想知道它是正确还是离开按钮,这样做:

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上)。
相关问题