5

Now I want to add a static pin annotation on map in my iOS app.

But I just want to know if the delegate method mapView:viewForAnnotation: will be called.

In the Apple documentation, it is said that

 When it needs an annotation view, the map view calls the mapView:viewForAnnotation:    method of its delegate object. 

I have read several tutorials from Internet and official documentation from Apple. And I still don't when this method will be called.

dannail
  • 455
  • 2
  • 10
  • 27

2 Answers2

11

Whenever you call addAnnotation method - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation gets called. Have look at the screenshot.

enter image description here

You can find this on MKMapView Class Reference

P.S. Put breakpoints at addAnnotation call and - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation you will see the flow of function call easily.

rohan-patel
  • 5,772
  • 5
  • 45
  • 68
  • 7
    Not complete answer, as viewForAnnotation may also be called when Map viewable region changes significantly. e,g: user zooms in on NYC, then zooms out, then zooms in on London. – AmitP Apr 07 '13 at 20:40
  • Very helpful!! WAs looking for this for an hour! – coolcool1994 Mar 14 '14 at 00:20
  • "Whenever you call addAnnotation method...": No. The doc screenshot doesn't say that and that's not what happens in practice. As the OP quoted the docs in the question, viewForAnnotation is called by the map view "When it needs an annotation view". If the map view is currently displaying the region in which you are adding your annotation, then viewForAnnotation _may_ get called immediately or soon after but there is no guarantee. As @AmitP also mentioned, viewForAnnotation can also be called multiple times _after_ annotation is already added if region changes. –  Apr 15 '15 at 13:26
2

Obviously MKMapView at least will call mapView:viewForAnnotation: when it will need to display your MKAnnotationView to the screen.

ivanzoid
  • 5,952
  • 2
  • 34
  • 43