6

通常、MKMapView 内に注釈を付けたいオブジェクト X があります。次のようにします。

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

次に、注釈ビューを内部に作成します

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

そして、一部の吹き出しがタップされると、内部でイベントを処理します。

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

Xを最新のタップイベントに渡す最もクリーンなソリューションは何ですか?

4

2 に答える 2

17

私があなたの質問を理解している場合は、calloutAccessoryControlTapped メソッドでオブジェクトにアクセスできるように、DDAnnotation クラスに参照またはプロパティを追加する必要があります。

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

注釈を作成する場合:

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

それで:

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

    DDAnnotation *anno = view.annotation;
    //access object via
    [anno.objectX callSomeMethod];
}
于 2010-02-26T04:30:34.420 に答える
0

私はこれを行い、うまくいきました!

マップがタップされたときに何かをする必要がありましたが、タップを注釈フローに正常に流す必要があったため、まさに私が必要としているものです。

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease];
    g.cancelsTouchesInView = NO;
    [self.mapView addGestureRecognizer:g];

}

- (void) handleGesture:(UIGestureRecognizer*)g{
    if( g.state == UIGestureRecognizerStateEnded ){
        NSSet *visibleAnnotations = [self.mapView annotationsInMapRect:self.mapView.visibleMapRect];
        for ( id<MKAnnotation> annotation in visibleAnnotations.allObjects ){
            UIView *av = [self.mapView viewForAnnotation:annotation];
            CGPoint point = [g locationInView:av];
            if( [av pointInside:point withEvent:nil] ){
                // do what you wanna do when Annotation View has been tapped!
                return;
            }   
        }
        //do what you wanna do when map is tapped
    }
 }
于 2012-10-23T15:14:02.640 に答える