MKPinAnnotationView-不同引脚中的不同动作 [英] MKPinAnnotationView - different actions in different Pins

查看:72
本文介绍了MKPinAnnotationView-不同引脚中的不同动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MKPinAnnotationView有疑问.首先,我输入了引脚的坐标,然后调用了viewForAnnotation来构建它们,并向它们添加一个右键.

I have a question about the MKPinAnnotationView. First of all I entered coordinates of the pins and after that called the viewForAnnotation to build them and also add them the a right button.

但是我的问题是如何为这些引脚选择不同的动作?

But my question is how can I select different actions for those pins?

当我在NSLog中寻找按钮标签时,每个引脚始终显示0,因此我无法使用标签来实现.

When I look for the button tag in NSLog, it always shows 0 for every pin so I can't make it with tags.

以下是按钮的代码(如果有含义的话):

Here is the code of the button if it means something:

for (int i=0;i<=[[mapview annotations]count];i++) {
    pinView.tag = i ;
    rightButton.tag=i;
}

推荐答案

您不需要(也不应该)使用标签.

You don't need to (and should not) use tags.

相反,在action方法中,您可以确定选择了哪个注释,然后基于该注释执行不同的逻辑.

Instead, in the action method, you can determine which annotation was selected and then execute different logic based on that.

您甚至不需要创建自己的操作方法.轻按一个标注按钮后,地图视图将调用其calloutAccessoryControlTapped委托方法,该方法为您提供对注释的引用(即view.annotation).如果决定使用委托方法,请从viewForAnnotation中删除addTarget并仅实现委托方法.

You don't even need to create your own action method. When a callout button is tapped, the map view will call its calloutAccessoryControlTapped delegate method which gives you a reference to the annotation (ie. view.annotation). If you decide to use the delegate method, remove the addTarget from viewForAnnotation and just implement the delegate method.

如果出于某种原因要使用自己的操作方法,则可以通过查看地图视图的selectedAnnotations属性来确定选择了哪个注释.选定的注释将位于索引0(请确保首先检查数组的count不为零).

If you want to use your own action method for some reason, you can determine which annotation was selected by looking at the map view's selectedAnnotations property. The selected annotation will be at index 0 (be sure to first check that the array's count is not zero).


有关以上所有示例代码,请参见以下问题:
如何保存数据


For sample code of all the above, see this question:
How to keep data associated with MKAnnotation from being lost after a callout pops up and user taps disclosure button?

这篇关于MKPinAnnotationView-不同引脚中的不同动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆