MKAnnotation Custom Callout button general action — how to know which Annotation it's coming from? (iPhone)
-
20-04-2021 - |
题
I'm developing a MapKit app which uses a whole bunch of MKAnnotations to mark locations. What I want is to put buttons into the callouts which the user can press to perform an action on the selected location. So far I have seen examples where you can put a specific handler onto each callout, like the apple sample code "MapCallouts".
I want to use one handler for all of the annotation callouts which is general purpose, where I would ideally pass in the MKAnnotation for the active callout.
I've searched and searched in Google to no avail, can anybody give me a pointer or a link?
Thanks!
解决方案
Instead of adding your own handler method using addTarget
and then using the selectedAnnotations
property to figure out which annotation was tapped, I prefer using the calloutAccessoryControlTapped
delegate method.
In that delegate method, the annotation tapped is accessed using view.annotation
and if you've defined a custom annotation class with additional properties, you can do a cast to access them using (MyAnnotationClass *)view.annotation
.