xcode 从未调用过 MKAnnotationView viewForAnnotation
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6412504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MKAnnotationView viewForAnnotation never called
提问by rockstarberlin
after i spend 2 days of searching the bug i have to ask for help here. i do have MapViewController and place some pins on the map. i′ve copied most of the code from MapCallouts and WeatherMap of the apple code samples.
在我花了 2 天的时间搜索错误后,我不得不在这里寻求帮助。我确实有 MapViewController 并在地图上放置了一些图钉。我已经从苹果代码示例的 MapCallouts 和 WeatherMap 中复制了大部分代码。
anyhow it seems that i′ve deleted or missed essential parts. it seems there is no connection between the MapViewController and the following code
无论如何,我似乎已经删除或遗漏了重要部分。MapViewController 和以下代码之间似乎没有联系
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"MKAnnotationView");
return nil;
}
setting an annotation looks like this and 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 figure out what's wrong. could anybody give me a hint what′s missing? i have to admit that i don′t have any experience with the delegate pattern.
我不知道出了什么问题。有人可以给我提示缺少什么吗?我不得不承认我对委托模式没有任何经验。
回答by
Make sure the map view's delegate
property is set.
确保设置了地图视图的delegate
属性。
If the map is created in IB, right-click on it and hook up its delegate outlet to File's Owner.
如果地图是在 IB 中创建的,请右键单击它并将其委托出口连接到文件所有者。
If the map is created in code, set the delegate after creating the map view:
如果地图是在代码中创建的,则在创建地图视图后设置委托:
mapView.delegate = self;
回答by Sylverb
In the .h where your MKMapView is declared and where your declare viewForAnnotation method, be sure to add MKMapViewDelegate in the list of protocols your class should include :
在声明 MKMapView 和声明 viewForAnnotation 方法的 .h 中,确保在类应包括的协议列表中添加 MKMapViewDelegate:
@interface myViewController : UIViewController <MKMapViewDelegate> {
MKMapView *_mapView;
}
Then in the viewDidLoad method, be sure to to add
然后在viewDidLoad方法中,一定要添加
_mapView.delegate = self;
You can also assign the delegate of the mapView in interface builder if you've done things using it !
如果您已经使用它完成任务,您还可以在界面构建器中分配 mapView 的委托!