Lucas Pereira
Lucas Pereira

Reputation: 569

Pass parameter on selector of UIButton

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

Upvotes: 1

Views: 4724

Answers (3)

NSPunk
NSPunk

Reputation: 502

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

Upvotes: 2

holex
holex

Reputation: 24041

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

Upvotes: 4

cloosen
cloosen

Reputation: 993

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

Upvotes: 0

Related Questions