从未调用过MKAnnotationView viewForAnnotation

roc*_*lin 7 iphone xcode delegates mkmapview ios

在我花了2天的时间搜索这个bug后,我必须在这里寻求帮助.我有MapViewController并在地图上放置一些引脚.我已经从AppleCallouts和WeatherMap复制了苹果代码示例中的大部分代码.

无论如何,我似乎删除或遗漏了必要的部分.似乎MapViewController和以下代码之间没有任何关联

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

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

- (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]];
}
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题.任何人都可以给我一些提示吗?我不得不承认我对委托模式没有任何经验.

小智 16

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

如果在IB中创建了地图,请右键单击它并将其委托出口连接到文件所有者.

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

mapView.delegate = self;
Run Code Online (Sandbox Code Playgroud)