Configuring an action for a custom subclass of UIButton

I’m trying for a couple of days to find a way to call an action: a selector on a custom button, obtained from UIButton, which I want to associate with a pin annotation. The reason I used the subclass is because I want to pass some data to this button object in order to show a different UIViewdisplay of this data when the button is clicked.

The problem is that at runtime, when I click on the output annotation, it opens its button, and when I click on it, nothing happens. My method is showDetails:not called when an event is received touchupInside, as I set here:

[rightButton addTarget:self 
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];

My question is how to inherit the methods UIButton, for example addTarget:action:forControlEvents:, buttonWithType:and so on, to use them in my subclass?

Here is the complete problem code:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKPinAnnotationView *annView=[[[MKPinAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:@"MyPin"] autorelease];  
    annView.animatesDrop=FALSE;  
    annView.canShowCallout = YES;  
    [annView setSelected:YES]; 

    if ([[annotation title] isEqualToString:@"Current Location"]) {
        annView.pinColor = MKPinAnnotationColorRed;  
    }
    else {
        annView.pinColor = MKPinAnnotationColorPurple; 
    }

    annView.calloutOffset = CGPointMake(-5, 5);  

    PlaceMark *pm=(PlaceMark *)annotation;
    AddressItem *ai=[pm getData];

    //Another problem is that i cant set buttonWithType:UIButtonTypeDetailDisclosure cause it generate error cause it is a method of UIbutton superclass
    MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeCustom];
    [rightButton setData:ai];

    [rightButton setTitle:annotation.title forState:UIControlStateNormal];

    [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];

    annView.rightCalloutAccessoryView = rightButton;

    return annView;  
}

Thanks in advance. I hope that I have given a clear explanation of my problem.

+3
source share
1 answer

I think you're wrong. To send an @selector action, your class must inherit from UIControl, not UIButton. Check out the UIControl Class Reference I think the problem is here. Feel free to tell me if I understand your question.

0
source

All Articles