objective-c 向 MKMapView 标注添加附件视图按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1433067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:13:16  来源:igfitidea点击:

Adding an accessory view button to the MKMapView call out annotation?

iphoneobjective-cannotationsmkmapview

提问by ingh.am

I've recently come across this websiteand I've been trying to add to my call out view a button (from a image).

我最近访问这个网站,我一直试图在我的呼叫视图中添加一个按钮(来自图像)。

The code on the websites example works just fine, but when I tried to add in the same code to my project I'm not getting the same results.

网站示例中的代码运行良好,但是当我尝试将相同的代码添加到我的项目时,却没有得到相同的结果。

There is obviously something I've missed but I cannot seem to work this one out!

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

Here is my code for annotating:

这是我的注释代码:

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

Any help would be appreciated! :)

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

采纳答案by ingh.am

Sorry to answer my own question but this was solved by setting the delegate of the MKMapView to the ViewController. Phew, took me a while to figure that out and it's so simple!!

很抱歉回答我自己的问题,但这已通过将 MKMapView 的委托设置为 ViewController 来解决。呼,我花了一段时间才弄明白,这太简单了!!

回答by Heath Borders

That guy is using a custom image to imitate the detail disclosure. You can actually get the standard detail disclosure quite easily:

那家伙正在使用自定义图像来模仿细节披露。您实际上可以很容易地获得标准的详细信息披露:

[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

回答by tony.tc.leung

Instead of setting the target like you did here:

而不是像你在这里做的那样设置目标:

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

You can use the default delegate:

您可以使用默认委托:

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