Suppose I have a button that I am adding to an annotation object in a mapview:
AnnotationButton* rightButton = [AnnotationButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
You will notice that the button calls the function showDetails when it is clicked.
Show details is defined as - (void)showDetails:(id)sender; and takes a sender. Is there a way to send more variables, or associate a different sender? The reason is that I want the button clicked to tell me which annotation is associated with that button. Consider the annotation to be some other object which is available during the context where the button is created.
I thought about subclassing the UIButton class, and then storing additional information within it, but that seems like a hack.
Any ideas?
If this button is being used for the
rightCalloutAccessoryVieworleftCalloutAccessoryViewof a MKAnnotationView, your map’s delegate should receive the messagemapView:annotationView:calloutAccessoryControlTapped:when the button is tapped. This hands you the MKAnnotationView instance that was tapped, which has anannotationproperty to give you the corresponding annotation. You should make use of that instead of trying to use an action on the button directly.