0

I am working on an mapkit app which drops pins on the map on certain locations (code at the end)

I have also added code for the callout. However, depending on which pin was tapped I was attemping to load a different views and I have no found any way to do oddly.

I placed a breakpoint and while stepping thru I noticed that there was a line which read. "annotation.title=@"myhome"; so i was wondering if there way someway i could say what to call depending on which annotation was tapped.

Thanks

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {
                     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

        annotationView.pinColor = MKPinAnnotationColorRed;
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;
    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    NSLog(@"Callout Tapped");
}

--Updated Code

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
  //  NSLog(annotation);

    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

        annotationView.pinColor = MKPinAnnotationColorRed;
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;
    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    id <MKAnnotation> annotation = [view annotation];
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
        {
    //    MKUserLocation *ann = (MKUserLocation *)view.annotation;
     //   NSLog(@"ann.title = %@", ann.title);
        NSLog(@"HJ");
    }
}
Sleep Paralysis
  • 449
  • 7
  • 22
  • 1
    In `calloutAccessoryControlTapped`, the annotation that was tapped is directly available using `view.annotation`. For example: `NSLog(@"Callout Tapped, title=%@", view.annotation.title);`. See http://stackoverflow.com/questions/4565197/how-to-find-which-annotation-send-showdetails, http://stackoverflow.com/questions/22572342/how-to-access-to-an-annotation-attribute-inside-method-calloutaccessorycontrolta, etc. –  Jun 10 '14 at 01:19
  • @Anna I have looked at the code. tried implementing it. I still cant get it too work thou. I click the discloser button and it doesnt run the if statement as if to say if ([annotation isKindOfClass:[MKPointAnnotation class]]) doesnt evaluate to true – Sleep Paralysis Jun 10 '14 at 04:18
  • @Anna Got it. Didnt notice i was mixing MKPointAnnotation with MapAnnotation (Class of mine) – Sleep Paralysis Jun 10 '14 at 04:45

1 Answers1

0

You could set tags say 1 for left and 2 for right accessory view. And in your calloutAccessoryControlTapped method, do like:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([control tag] == 1) {
        NSLog(@"tapped left accessory");
    }
    else if ([control tag] == 2) {
        NSLog(@"tapped right accessory");
    }
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162