0

I have an UIViewController class to manage an MKMapView defined like this:

@interface MapViewController : UIViewController <MKMapViewDelegate>

Within the implementation of this view controller, I have:

- (void)loadView
{
   MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
   mapView.delegate = self;
   self.view = mapView;
}


- (void)centerMapInLoc:(CLLocation *)loc
{
   CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinates:loc.coordinate title:@"My position" subTitle:nil];

   [(MKMapView *)self.view addAnnotation:annotation];
}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
   MKAnnotationView *v = nil;

   // Custom logic

   v.annotation = annotation;
   return v;
}

Being centerMapInLoc: a public method I call from another view controller. The annotation is created within such method, but viewForAnnotation: delegate method is not called when the annotation is added. I don't understand why, since the delegate is set in loadView method.

Thanks in advance

AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • Are you viewing in the map the coordinates of the annotation??, `viewForAnnotation` will only execute if the annotation is going to appear on the map. Move to the portion of the map that should show the annotation and verify that the method is being called – gabuh Jan 14 '15 at 09:01
  • @gabuh Yes, I'm displaying the appropriate region, in fact if I add the annotation within `viewDidLoad` method, the `viewForAnnotation` delegate method is called and annotation displayed... – AppsDev Jan 14 '15 at 09:07
  • And are you sure that self.view is initialized when you are calling centerMapInLoc from the other viewController? NSLog self.view in centerMapInLoc. – gabuh Jan 14 '15 at 10:02
  • @gabuh Yes, it is... and delegate seems to be different from nil as well... – AppsDev Jan 14 '15 at 10:12
  • 1
    try adding the mapView as child of the main view in your viewController, instead of replacing it. `[self.view addSubview:mapView]` you can keep a reference to the mapView in your viewController (add a MKMapView property) to help you add the annotation in centerMapInLoc – gabuh Jan 14 '15 at 10:18

0 Answers0