我有一个地图,在其标注的标注上有一个公开按钮,点击后会显示详细视图。地图上的每个点都需要在详细视图中加载不同的数据 - 我不想为每个注释构建单独的视图,原因很明显,但我不确定这样做的最佳方法。
如何标记单击了哪个注释,以便通过详细信息的视图控制器加载正确的数据?
答案 0 :(得分:2)
您可以在注释类中实现数据加载功能。为此,您需要在注释类中引用该数据。
您的注释类可以是
@interface MyAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
MyDataObject *mydataObject;
}
- (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject;
- (void) loadDetailView; // this function will load the data in myDataObject to the DetailView
要获得标注触摸,您可以在实现MKMapViewDelegate的控制器中实现calloutAccessaryControlTapped函数。
该功能将类似于
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
MyAnnotation *myAnnotation = (MyAnnotation*)view.annotation;
[myAnnotation loadDetailView];
}
initWithCoordinate的实现将类似于:
@implementation MyAnnotation
....
- (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject{
coordinate = coord; // cannot use "self.coordinate" here as CLLocationCoordinate2D is double instead of object (?)
self.title = <# some name #> // assign some string to the title property
self.mydataObject = aDataObject; // this is your data object
return self;
}
....