问题
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?
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;
}
回答1:
You don't need to (and should not) use tags.
Instead, in the action method, you can determine which annotation was selected and then execute different logic based on that.
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.
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?
来源:https://stackoverflow.com/questions/9812471/mkpinannotationview-different-actions-in-different-pins