【问题标题】:MKAnnotationView and passing data to another functionMKAnnotationView 并将数据传递给另一个函数
【发布时间】:2010-12-22 13:33:50
【问题描述】:

实际上,我正在我的应用程序中开发一些东西,它在我的MKMapView 中显示注释,这些注释应该是可点击的,并将参数传递给方法。
现在的问题是如何在点击显示按钮时将参数传递给下一个函数。
对于注释的格式,我使用- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation 方法来显示披露按钮。但问题是,如何从当前显示的Annotation 对象中获取值?

我的Annotation 代码(仅相关部分):

@synthesize coordinate, id, title, subtitle;

+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coordinate {
    return [[[[self class] alloc] initWithCoordinate:coordinate] autorelease];
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
    self = [super init];

    if(nil != self) {
        self.coordinate = coordinate;
    }

    return self;
}

当触摸披露按钮时,应调用方法- (void)displayInfo:(NSNumber *) anId;,并将Annotation.id作为anId参数移交。

使用该代码生成披露按钮:

   UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    dropPin.rightCalloutAccessoryView = disclosureButton;
    dropPin.animatesDrop = YES;
    dropPin.canShowCallout = YES;

所以 - 问题:
如何将该参数传递给函数或读取点击的Annotationid

我认为我可以使用 UIButton 的选择器(但是 - 如何传递 id?)或使用 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control (再次 - 如何传递 id?)来处理对公开按钮的点击。

我希望您知道我的意思,并且非常感谢您的回答! :)

【问题讨论】:

  • 另外,在您的注释类中,您似乎已经声明了一个名为“id”的属性?强烈建议您将其命名为其他名称,正如 ennukiller 回答的那样,您甚至可能不需要它。
  • 这是个好主意 ;-),你是对的。感谢您的帮助输入:)。

标签: iphone cocoa-touch mkmapview mkannotation mkannotationview


【解决方案1】:

在 calloutAccessoryControlTapped 中,注释可通过视图获得:

MKAnnotation *annotation = view.annotation;

【讨论】:

  • 这真的帮助了我!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2012-09-25
  • 2020-10-03
  • 1970-01-01
相关资源
最近更新 更多