views:

133

answers:

1

I have a map with a disclosure button on the callout of it's annotations, which when clicked displays a detail view. Each point on the map needs to load up different data in the detail view - I dont want to build separate views for each annotation, for obvious reasons, but I am unsure of the best way to do this.

How do I flag which annotation has been clicked, so I can load the correct data through the detail's view controller?

+2  A: 

You can implement the data loading function in your annotation class. To do that you will need to have a reference to that data in your annotation class.

Your annotation class can be something like

@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

To get the callout touch, you can implement calloutAccessaryControlTapped function in the controller where you implement MKMapViewDelegate.

The function will be something like

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
      MyAnnotation *myAnnotation = (MyAnnotation*)view.annotation;
      [myAnnotation loadDetailView];
}

Edit

The implementation of initWithCoordinate will be something like:

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

....
tmin
Thanks tmin.Excuse my ignorance, but I can't find any documentation about the data part of the solution. Is withMyData: a standard function? Could you please explain how the data function works I don't get how it is used...
daidai
withMyData: is not a standard function parameter. I included that parameter to pass in the data object reference so that I can use it later in the loadDataView method. Please see my edit for the initWithCoordinate function implementation. You can google on how to use MKAnnotation and MKMapView as well.
tmin