0

我是 iphone 开发的新手,我正在尝试将自定义按钮添加到注释标注中。我向 rightCalloutAccessoryView 添加常规按钮没有问题,但它不适用于自定义样式。我的图像大小是 32 x 32。我还想在此 stackoverflow 问题之后添加一个自定义地图图钉非常感谢任何帮助。

我的代码

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = @"MapPoint";   

if ([annotation isKindOfClass:[MapPoint class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;
    annotationView.image = [UIImage imageNamed:@"myimage.png"];

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setImage:[UIImage imageNamed:@"phony2.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = rightButton;

    return annotationView;
}
return nil;    

}

4

1 回答 1

2
 [rightButton addTarget:self
                action:@selector(showDetails:)

删除此行并设置按钮的框架

[rightButton setFrame:CGRectMake(0,0,32,32)];

并从 degate 方法中获取点击动作

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;

您在哪里获得注释视图。

希望这会帮助你。

于 2013-02-28T13:38:43.090 回答