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

问题描述:

我开始用iOS6构建一款运行良好的应用程序,但之后出于强制磨损的原因,我不得不切换到IOS5。但是,有一张地图不断给我提出问题。这张地图有许多类型的annotationView(例如电影院,餐馆,剧院......),每个都有自己的图像。当我从iOS6传递到iOS5时,我注意到annotationView的行为与以前不一样,因为调用委托方法来构造它们不再一样。我能做什么?ViewForAnnotation在iPad 6.0模拟器中工作,但在iPad 5.1模拟器中不起作用

-(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工作和代码是相同的

+0

给一些更多的代码或做一些示例代码...所以我们可以检查您的代码在我们的末尾.. – Rajneesh071

我很惊讶地听到,viewForAnnotation在每次施工后只会被调用。这不是它记录的方式,也不是其他人使用的方式。 viewForAnnotation可以在您的应用程序的任意位置调用。如果用户滚动地图以便一些注释消失,则可以在他们滚动地图并且他们的注释重新出现时被调用。或者,如果用户切换到另一个视图或应用程序,然后回来。

你需要让viewForAnnotation检查annotation变量某些属性,让你决定如何得出这样的观点。你不能依靠它以任何特定顺序被调用。有很多示例代码会告诉你如何在你自己的类中实现MKAnnotation协议,并且你可以添加一些东西给这个类来告诉你它代表的东西是电影院还是餐厅或者其他东西,然后你可以获得正确的图像把它放入希望作为回报值的MKAnnotationView

+0

我在哪里可以找到示例? –

+0

我想链接到l​​mgtfy,但是SO会阻止它。只是谷歌“MKAnnotation协议示例” – Craig

viewForAnnotation在通过循环运行时(即,因为您添加了每个MKAnnotation)而没有被调用,因为您位于主要的runLoop上。当你的viewDidLoad完成和runLoop周期发生时,MKAnnotation(在当前地图位置的那些)将被绘制在屏幕上,因此呼叫到你的delegate。因此,当您正在调试并通过viewDidLoad时,您无法看到对delegate方法的调用是非常正常的。

几件事情:

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

  • 您正在重新使用i作为for...loop变量。我希望这只是由于您在此删除业务逻辑而引起的,而不是在您的实际代码中。这将解释为什么只有最后一组注释呈现出来。

你确定你是不是在嵌套for...loops操纵mapView.annotations其他地方?检查removeAnnotationssetAnnotations:都可能导致mapView中的注释与您所期望的不同。

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

+0

我已经在第二个周期中创建了注释。 removeAnnotation和setAnnotation,我从来没有使用 –

你的代码似乎对我好。其实我也在开发一个使用Google地图的iPhone应用程序,我正在像你一样。只有一个问题,你为什么使用:

if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone) 

它是检查用户是否运行iPad或iPhone?

+0

我问,因为我总是使用NSString * deviceType = [UIDevice currentDevice] .model; –