我最近遇到了这个网站我一直试图在我的呼叫中添加一个按钮(来自图像)。

网站上的代码示例工作正常,但当我尝试将相同的代码添加到我的项目中时,我得不到相同的结果。

显然我错过了一些东西,但似乎无法解决这个问题!

这是我的注释代码:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor = MKPinAnnotationColorGreen;

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeCustom];
    advertButton.frame = CGRectMake(0, 0, 23, 23);
    advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [advertButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
    [advertButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];

    annView.rightCalloutAccessoryView = advertButton;

    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

任何帮助将不胜感激! :)

有帮助吗?

解决方案 3

很抱歉回答我自己的问题,但这是通过将MKMapView的委托设置为ViewController来解决的。 Phew,花了我一段时间来弄清楚它是如此简单!!

其他提示

那家伙正在使用自定义图片来模仿细节披露。您可以非常轻松地获得标准的详细信息:

[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

而不是像你在这里设定目标一样:

[advertButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];

您可以使用默认委托:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top