简体   繁体   中英

How to get reference on annotation via sender?

I connect annotation with 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

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 .
To cast correctly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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