简体   繁体   English

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

[英]How to get reference on annotation via sender?

I connect annotation with UITapGestureRecognizer. 我将注释与UITapGestureRecognizer连接。 I want to detect touch. 我想检测触摸。

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

I use the code bottom, but it caused error in compilation 我使用代码底部,但在编译时导致错误

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

ERROR: property view not found on object of type __strong id 错误:在类型为__strong id的对象上找不到属性视图

Change: 更改:

-(void) calloutTapped:(id) sender

to: 至:

-(void) calloutTapped:(UITapGestureRecognizer *) sender

then the sender would have the view property or first cast the sender to UITapGestureRecognizer . 那么sender将具有view属性, 或者首先将senderUITapGestureRecognizer
To cast correctly: 正确投放:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM