MKAnnotationView viewForAnnotation never called

after I spend 2 days searching for an error, I have to ask for help here. I have a MapViewController and put some contacts on the map. I copied most of the code from MapCallouts and WeatherMap sample Apple code.

In any case, it seems that I deleted or skipped the main parts. there seems to be no connection between MapViewController and the following code

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation { NSLog(@"MKAnnotationView"); return nil; } 

annotation setup looks like this: it works well:

 - (void)createPoi:(CLLocationCoordinate2D)theCoordinate { NSLog(@"createPoi"); RandomAnnotation *randomAnnotation = [[RandomAnnotation alloc] init]; [randomAnnotation setTheCoordinate:theCoordinate]; [randomAnnotation setTheTitle:@"bla"]; [randomAnnotation setTheSubTitle:@"bla"]; [self.mapAnnotations insertObject:randomAnnotation atIndex:kRandomAnnotationIndex]; [randomAnnotation release]; [self.mapView addAnnotation:[self.mapAnnotations objectAtIndex:kRandomAnnotationIndex]]; } 

I can’t understand what happened. Can someone give me a hint what is missing? I must admit that I have no experience with a delegate template.

+7
source share
2 answers

Make sure the map view delegate property is set.

If the card was created in IB, right-click on it and connect its delegate output to the file owner.

If the map is created in code, set the delegate after creating the map view:

 mapView.delegate = self; 
+16
source

In .h, where your MKMapView is declared and where you declare the viewForAnnotation method, be sure to add MKMapViewDelegate to the list of protocols your class should include:

 @interface myViewController : UIViewController <MKMapViewDelegate> { MKMapView *_mapView; } 

Then in the viewDidLoad method, be sure to add

 _mapView.delegate = self; 

You can also assign a mapView delegate in the interface builder if you did something with it!

+2
source

All Articles