MKAnnotation Custom Callout button general action — how to know which Annotation it's coming from? (iPhone)

StackOverflow https://stackoverflow.com/questions/9046337

質問

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top