如何通过发件人获取注释参考?

时间:2014-02-12 12:23:52

标签: ios objective-c map mkmapview

我使用UITapGestureRecognizer连接注释。 我想检测触摸。

    if ([annotation isKindOfClass:[Annotation class]])
 {
     NSString * annotationIdentifier = @"UserAnnotationIdentifier";
     CustomAnnotationView * customAnnotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
     if (!customAnnotationView)
     {
         customAnnotationView = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
         UITapGestureRecognizer *tapGesture =
         [[UITapGestureRecognizer alloc] initWithTarget:self
                                                 action:@selector(calloutTapped:)];
         [customAnnotationView addGestureRecognizer:tapGesture];

我使用代码底部,但它在编译中导致错误

  -(void) calloutTapped:(id) sender {
       id<MKAnnotation> annotation = ((MKAnnotationView*)sender.view).annotation;

错误:在__strong id

类型的对象上找不到属性视图

1 个答案:

答案 0 :(得分:0)

变化:

-(void) calloutTapped:(id) sender

为:

-(void) calloutTapped:(UITapGestureRecognizer *) sender

然后sender将具有view属性 首先将sender投放到UITapGestureRecognizer 正确施法:

 -(void) calloutTapped:(id) sender {
    UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)sender;
    MKAnnotationView *sendersView = (MKAnnotationView *)tapGesture.view;
    id<MKAnnotation> annotation = sendersView.annotation;
}