干净的解决方案来知道哪个 MKAnnotation 已被窃听?

发布于 2024-08-22 15:32:28 字数 607 浏览 4 评论 0原文

好的,所以您通常有一些想要在 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 传递到最新点击事件的最干净的解决方案是什么?

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

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

Then you create the annotation view inside

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

And when some callout gets tapped, you handle the event inside:

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

What's the cleanest solution to pass X down to the latest tap event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在夏天 2024-08-29 15:32:28

如果我理解您的问题,您应该向 DDAnnotation 类添加引用或属性,以便在 calloutAccessoryControlTapped 方法中您可以访问该对象。

@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];
}

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 <MKAnnotation> {
    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];
}
初熏 2024-08-29 15:32:28

我这样做了并且效果很好!

这正是我所需要的,因为我需要在点击地图时执行某些操作,但让点击正常进入注释流。

- (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
    }
 }

I did this and it worked alright!

It's exactly what I need because I needed to do something when the map was tapped but letting the tap into the annotation flow normally.

- (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
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文