我有很多自定义注释,我给了它属性(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
}
答案 0 :(得分:2)
在viewForAnnotation中,您需要告诉编译器注释实际上是AnnotationCustom对象。
所以你首先需要这样做:
AnnotationCustom *annotationCustom = (AnnotationCustom *)annotation;
然后尝试访问nameTable属性..
在didSelectAnnotationView方法中,如果您想要获取所选注释的nameTable值,则需要执行以下操作:
AnnotationCustom *annotationCustomSelected = (AnnotationCustom *)view.annotation;
NSLog(@"table name of annotation selected: %@", annotationCustomSelected.nameTable);