1

I have a detailDisclousure button on the callout of a MKAnnotation. When this button is pressed I need to call a method passing a parameter that identifies the annotation that called it. How could it be done?

this is my viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

if ([annotation isKindOfClass:[Annotation class]])
{
    static NSString* identifier = @"identifier";

    MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (pinView == nil) 
    {
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]autorelease];
    }

    Annotation *antt = annotation;

    pinView.canShowCallout = YES;
    pinView.draggable =    YES;

    UIButton* detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    [detailButton performSelector:@selector(goToViewWithAnnotation:) withObject:antt];

    pinView.rightCalloutAccessoryView = detailButton;

    return pinView;
}

else 
{
    return nil; 
}
}

And this is the method that should be called with the parameter:

-(void)goToViewWithAnnotation:(Annotation *)annotation
{
    NextViewController *nextView = [[NextViewController alloc]initWithNibName:@"NextViewController" bundle:nil];

nextView.id = annotation.id;

[self.navigationController nextView animated:YES];
}
Lucas Pereira
  • 569
  • 1
  • 11
  • 23

3 Answers3

4

you can pass any NSInteger via the tag property of the UIButton only.

holex
  • 23,961
  • 7
  • 62
  • 76
2

It's not possible to send data with the -addTarget:action:forControlEvent: for UIButton:

[detailButton addTarget:self action:@selector(goToViewWithAnnotation:) forControlEvents:UIControlEventTouchUpInside];

Try to handle the data with some other way, when you trigger the selector. Like saving a pointer or var as you like.

UIButton have these callers as you can see:

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
holex
  • 23,961
  • 7
  • 62
  • 76
NSPunk
  • 502
  • 4
  • 14
  • oh.. and btw, possible answer here: http://stackoverflow.com/questions/5690283/passing-custom-data-in-uibutton-addtarget – NSPunk Jul 23 '12 at 14:02
0

I think you can add a property(which is (Annotation *)annotation) in .h file. Then you can use annotation in the "-(void)goToViewWithAnnotation" method. Or you can customized a new button which inherits UIControl

cloosen
  • 993
  • 7
  • 17