Setting action for custom UIButton subclass
I have been trying for a couple of days to find a way how to call action:selector on a custom button, derived from UIButton
, which I want to associate with a pin annotation. The reason that I used a subclass is that I want to pass some data into this button object, in order to show another UIView
displaying these data when the button is pressed.
The problem is that while executing, when I click on the pin annotation, it opens its button, and when I click on it, nothing happens. My showDetails:
method is not called on receiving the touchupInside
event as I set here:
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
My question is how to inherit the methods of UIButton
, like addTarget:acti开发者_JAVA技巧on:forControlEvents:
, buttonWithType:
, etc., in order to use them in my subclass?
Here is the complete code concerning the problem:
- (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;
}
Thank you in advance. I hope that I have given a clear explanation of my problem.
I think you're on wrong way. To send action @selector your class need inherit from UIControl and not UIButton. Check out UIControl Class Reference I think the problem is here. Do not hesitate to tell me if I did not understand your question.
精彩评论