gpt4 book ai didi

ios - 自定义注释的属性在委托(delegate)方法中不可见

转载 作者:行者123 更新时间:2023-11-28 22:45:34 25 4
gpt4 key购买 nike

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

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

//.....
}

但是在委托(delegate)方法中:

  (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


}

最佳答案

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

所以你首先需要这样做:

AnnotationCustom *annotationCustom = (AnnotationCustom *)annotation;

然后尝试访问 nameTable 属性..

在didSelectAnnotationView方法中,如果你想获取选中注解的nameTable值,需要做如下操作:

AnnotationCustom *annotationCustomSelected = (AnnotationCustom *)view.annotation;
NSLog(@"table name of annotation selected: %@", annotationCustomSelected.nameTable);

关于ios - 自定义注释的属性在委托(delegate)方法中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13305949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com