MKAnnotationView viewForAnnotation从来没有调用过

我花了2天的时间寻找错误之后,我必须在这里寻求帮助。 我确实有MapViewController,并在地图上放置一些引脚。 我从苹果代码示例的MapCallouts和WeatherMap中复制了大部分代码。

无论如何,似乎我已经删除或错过了重要的部分。 似乎MapViewController和下面的代码之间没有连接

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

设置一个注释看起来像这样,它运作良好:

 - (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]]; } 

我无法弄清楚什么是错的。 有人可以给我一个暗示什么是缺less的? 我不得不承认,我没有任何与委托模式的经验。

确保地图视图的delegate属性已设置。

如果地图是在IB中创build的,请右键单击它并将它的代理sockets连接到文件所有者。

如果地图是用代码创build的,请在创build地图视图后设置委托:

 mapView.delegate = self; 

在.h你的MKMapView被声明的地方,你声明viewForAnnotation方法的地方,一定要在你的类应该包含的协议列表中添加MKMapViewDelegate:

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

然后在viewDidLoad方法中,一定要添加

 _mapView.delegate = self; 

你也可以在界面生成器中分配mapView的委托,如果你已经做了使用它的东西!