我开始用 iOS6 构建一个运行良好的应用程序,但后来由于强制磨碎器的原因,我不得不切换到 IOS5。但是,有一张 map 不断给我带来问题。这张 map 有多种类型的annotationView(例如电影院、餐厅、剧院……),每一种都有自己的图像。当我从 iOS6 传递到 iOS5 时,我注意到 annotationView 的行为与以前不同,因为构造它们的委托(delegate)方法的调用不再相同。我能做什么?

-(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

   }

  }

委托(delegate)方法
  - (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;
            }

            //......

  }

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

最佳答案

我很惊讶地听到 viewForAnnotation 只在每次构建后才被调用。这不是记录的方式,也不是其他任何人使用的方式。 viewForAnnotation 可以并且将在您的应用程序中的任何时候调用。如果用户滚动 map 使某些注释消失,则可以在他们向后滚动 map 并重新出现注释时调用它。或者,如果用户切换到另一个 View 或应用程序,然后又回来了。

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

关于ios - ViewForAnnotation 在 iPad 6.0 模拟器中工作,但在 iPad 5.1 模拟器中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13400270/

10-11 04:35