0

我有许多自定义注释,我向其提供了属性(NameTable 和 ID)。我在 AddAnnotation 创建的那一刻之前设置了这个属性,但是这些属性在委托方法中不再可见。我有多个注释与从数据库中获取的表元素相关联。如何使它们在委托方法中可见?

 - (void)viewDidLoad
{

     //......

   for(int i=0; i<6; i++){ //loop for create multiple annotations

   AnnotationCustom *annotationIcone =[[AnnotationCustom alloc]initWithCoordinates:coord 
               title:self.myTable.title subTitle:self.myTable.address];

        annotationIcone.nameTable = [NSString stringWithFormat:@"%@", self.myTableName];
        annotationIcone.ID = i+1;

    [self.mapView addAnnotation: annotationIcone;

     //.....
   }

但是在委托方法中:

  (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
   <MKAnnotation>)annotation
    {

     NSLog(@"The name of table is:@"%@", annotation.nameTable);
     //property 'nameTable' not found on object of type '_strong id <MKAnnotation>

     NSLog (@The name of table is:@%@", annotation.ID);
     //property 'ID' not found on object of type '_strong id <MKAnnotation>

         //......
     }

在另一种方法中:

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

       NSLog(@"The name of table is %@", self.myTableName);
       // here I get the name of last table open and not the name of table selected


      }
4

1 回答 1

2

在 viewForAnnotation 中,您需要告诉编译器该注解实际上是一个 AnnotationCustom 对象。

所以你首先需要这样做:

AnnotationCustom *annotationCustom = (AnnotationCustom *)annotation;

然后尝试访问 nameTable 属性..

在 didSelectAnnotationView 方法中,如果您想要获取所选注解的 nameTable 值,您需要执行以下操作:

AnnotationCustom *annotationCustomSelected = (AnnotationCustom *)view.annotation;
NSLog(@"table name of annotation selected: %@", annotationCustomSelected.nameTable);
于 2012-11-09T10:23:33.347 回答