ViewForAnnotation 在 iPad 6.0 模拟器中工作,但在 iPad 5.1 模拟器中不起作用



我开始用iOS6构建一个运行良好的应用程序,但后来由于强制刨丝器的原因,我不得不切换到IOS5。但是,有一张地图一直给我带来问题。此地图具有多种类型的注记视图(例如电影院、餐馆、剧院等),每种类型都有自己的图像。当我从 iOS6 转到 iOS5 时,我注意到注释视图的行为与以前不同,因为委托方法来构造它们的调用不再相同。我能做什么?

-(void)viewDidLoad{
//.....
//extraction of elements from the tables in a database
for(int i=0; i<7; i++){ //extraction tables from database
//......
for (int i =0; i< number; i++) { //extraction elements from tables
self.chinaTable =[self.mutableArray objectAtIndex:i];
CLLocationCoordinate2D coord=CLLocationCoordinate2DMake(self.chinaTable.latitudine, self.chinaTable.longitudine);
AnnotationCustom *annotationIcone =[[AnnotationCustom alloc]initWithCoordinates:coord title:self.chinaTable.titolo subTitle:self.chinaTable.indirizzo];
//.......
[self.mapView addAnnotation:annotation];
self.locationManager.delegate = self;
self.mapView.delegate=self; //the problem is here
}
}

委托方法

- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
<MKAnnotation>)annotation 
{
NSLog (@"the name of the table is %@", self.nomeTabella);
// this NSLog you get only the name of the last open table
//..........
if ([annotation isKindOfClass:[AnnotationCustom class]]){
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = (MKAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
annotationView.annotation = annotation;
AnnotationCustom *customAnnotation = (AnnotationCustom *)annotationView.annotation;
//I create an annotation different depending on the name of the table of provenance
if ([self.nomeTabella isEqualToString:@"Cinema"]){
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
annotationView.image = [UIImage imageNamed:@"iphone_cinema.png"];
customAnnotation.nomeTabella = self.nomeTabella;
customAnnotation.ID = self.chinaTable.ID;
customAnnotation.china = self.chinaTable;
}else{
annotationView.image = [UIImage imageNamed:@"ipad_cinema.png"];
customAnnotation.nomeTabella = self.nomeTabella;
customAnnotation.ID = self.chinaTable.ID;
customAnnotation.china=self.chinaTable;
}
//......
}

委托方法viewForAnnotation在每个注释的构造后不再调用,而仅在两个周期结束时调用,因此映射上的annotationView只是内存中最后一个表的那些。我在哪里可以设置委托方法以获得与以前相同的结果?ViewForAnnotation在iPad 6.0模拟器中工作正常,但在iPad模拟器5.1中不起作用,代码相同

我很惊讶地听到viewForAnnotation只在每次构造后才被调用。这不是记录它的方式,也不是其他人使用它的方式。viewForAnnotation可以并且将在应用中的任何点调用。如果用户滚动地图,使某些注记消失,则可以在向后滚动地图时调用该注记,并且注记会重新出现。或者,如果用户切换到另一个视图或应用,然后返回。

您需要viewForAnnotation检查annotation变量中是否有某些属性,以便确定如何绘制该视图。您不能依赖以任何特定顺序调用它。周围有很多示例代码将向您展示如何使用自己的类实现MKAnnotation协议,您可以向该类添加一些内容来告诉您它所代表的东西是电影院还是餐馆或其他什么,然后您获取正确的图像将其放入viewForAnnotation想要作为返回值的MKAnnotationView中。

当您运行循环时(即当您添加每个MKAnnotation时),不会调用viewForAnnotation,因为您在主runLoop上。当您的viewDidLoad完成并发生runLoop周期时,屏幕上将绘制MKAnnotations(当前地图位置上的s),从而向您delegate呼唤。因此,在调试和单步执行viewDidLoad时,看不到对delegate方法的调用是完全正常的。

几件事:

  • 您应该在循环之外设置委托方法。 因此,self.locationManager.delegateself.mapView.delegate应该在for循环之前设置,而不是在嵌套循环中设置一百万次。

  • 您正在重用i作为for...loop变量。我希望这只是由于您删除了在此处发布的业务逻辑,而不是在您的实际代码中。它可以解释为什么似乎只呈现了最后一组注释。

您确定没有在嵌套for...loops中的其他地方操作mapView.annotations吗?检查removeAnnotationssetAnnotations:两者都可能导致mapView中的批注与预期不同。

你能展示你的annotation对象是如何构造的吗?另一种可能性是你不小心改变了现有对象,而不是创建新对象。

你的代码对我来说似乎还可以。实际上,我也在使用地图开发一个Iphone应用程序,我和你一样做。只有一个问题,你为什么要使用:

if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone)

是检查用户运行的是iPad还是iPhone?

最新更新