Clean solution to know which MKAnnotation has been tapped?

前端 未结 2 1601
醉话见心
醉话见心 2021-02-09 20:08

Ok, so you typically have some object X you want to be annotated inside a MKMapView. You do this way:

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCo         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 20:43

    If I'm understanding your question, you should add a reference or property to your DDAnnotation class so that in your calloutAccessoryControlTapped method you can access the object.

    @interface DDAnnotation : NSObject  {
        CLLocationCoordinate2D coordinate;
        id objectX;
    }
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    @property (nonatomic, retain) id objectX;
    

    When you create the annotation:

    DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate:poi.geoLocation.coordinate title: @"My Annotation"];
    annotation.objectX = objectX;
    [_mapView addAnnotation: annotation];
    

    Then:

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
    
        DDAnnotation *anno = view.annotation;
        //access object via
        [anno.objectX callSomeMethod];
    }
    

提交回复
热议问题