2011-06-20 41 views
7

我花了2天的搜索错误后,我不得不寻求帮助。我确实有MapViewController并在地图上放置了一些针脚。我已经从Apple代码示例的MapCallouts和WeatherMap中复制了大部分代码。无论如何,似乎我已经删除或错过了重要部分。它似乎还有以下代码MKAnnotationView viewForAnnotation从来没有呼吁

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

设置的注释是这样的的MapViewController之间没有连接,而且运作良好:

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

我不能弄清楚什么是错的。有人可以给我一个提示什么是缺少的?我不得不承认,我对代表模式没有任何经验。

回答

16

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

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

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

mapView.delegate = self; 
+0

aah。是!我忘了把IB的地图挂到文件的所有者代表。耻辱我: -/ 问题解决了! 非常感谢您的快速帮助! :-) – rockstarberlin

2

在.H在您的MKMapView声明,并在您的申报viewForAnnotation方法,一定要加MKMapViewDelegate在协议列表类应包括:

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

然后在viewDidLoad方法时,一定要加

_mapView.delegate = self; 

如果您已经使用过它,您还可以在界面构建器中分配mapView的代理!