I have a map view with pins that when the user selects a pin it goes to a detail screen for that pin. I also have a table view that when the user selects an item it goes to the
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
myAnnotation *annView = view.annotation; // myAnnotation is my custom class for annotations
// now in annView you have the exact annotation whose accessorybutton is clicked now you can do whatever you want with it.
NSLog(@"title = %@",annView.title);
}
Rather then setting the button tag you can use the title or subtitle of the annotation for futher comparision or whatever you want
In your code you are using
[newAnnotation addSubview:pinButton]; // i think this is wrong you should only add it to accessory view
In viewForAnnotation
, the way you are setting the button tag using an ivar counter (indexvar
) assumes that the delegate method will get called only once and in the order that the annotations are added (which you are presumably doing in index order in some loop).
The above assumption is not safe to make and not recommended. For example, the delegate method can definitely be called multiple times for the same annotation if the map view needs to re-display an annotation after it comes back into view because the user panned or zoomed back.
Instead of using button tags, embed the information you need into the annotation class itself and set that information when you add the annotation.
At a minimum (but still not recommended), what you could do is add a propertyIndex
property to your annotation class and set it equal to the array index (of say the PropertyReferenceArr
array) you are creating the annotation from.
A better solution, based on the numerous arrays you are using in calloutAccessoryControlTapped
, is to create a proper class object to hold all the properties (of your real-estate "Properties" objects) and then have a single array to hold them (instead of a separate array for each attribute).
This class could itself conform to MKAnnotation
so you don't need to create a separate, explicit annotation object (you can just add the property object itself to the map).
Then in calloutAccessoryControlTapped
, you just cast view.annotation
to your custom class and you instantly have access to all the annotation/Property data without any need for a tag or array index.
The comment @naveen makes about your addSubview
line is also valid (though it's not related to the issue). You should definitely remove that.
Also, in viewForAnnotation
, calling [pinButton release];
after the return
is pointless. That code will never run (which is good because if you put it before the return
, app will crash since pinButton
is autoreleased).