![your your](https://c1.lmlphp.com/image/static/default_avatar.gif)
本文介绍了清洁解决方案知道哪个MKAnnotation已经被窃听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,所以你通常有一些对象X你想在MKMapView注释。您这样做:
DDAnnotation * annotation = [[DDAnnotation alloc] initWithCoordinate:poi.geoLocation.coordinate title:@My注解];
[_mapView addAnnotation:annotation];
然后在
中创建注释视图
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id< MKAnnotation>)annotation;
当一些标注被点击时,你可以处理这个事件:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
将X传递到最新点击事件的最干净的解决方案是什么?
$ b $如果我理解你的问题,你应该添加一个引用或属性到你的DDAnnotation类,以便在你的calloutAccessoryControlTapped方法中,你可以访问该对象。 / p>
@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;
//通过
访问对象[anno.objectX callSomeMethod];
}
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?
解决方案
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];
}
这篇关于清洁解决方案知道哪个MKAnnotation已经被窃听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!