简体   繁体   中英

How to track which annotation callout clicked

I am creating an annotation callout with right accessory button, using the following code

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

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

else {

    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    }

    annotationView.image = [UIImage imageNamed:@"Mevents.png"];
    annotationView.annotation = annotation;
    annotationView.canShowCallout=YES;

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];

    annotationView.rightCalloutAccessoryView = rightButton;

    //  UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
    //  pinView.leftCalloutAccessoryView = profileIconView;
    //  [profileIconView release];

    return annotationView;

}

}

How can I track, which annotation is clicked ? I want to load the details screen with the id and fetch the data based on that id to show info.

When you create the annotation add a tag to it

annotationView.image = [UIImage imageNamed:@"Mevents.png"];
//Add this after
annotationView.tag = 111;

Now in viewForAnnotation , check for this tag

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

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

    if(annotation.tag == 111)
        //Do something
    else
        //Do some other thing
}

hey bro check my answer in the following thread it can help you.

Pass data to detailView when annotation tapped on mapview

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