I created a CustomButton why do I need to pass as parameter in a @selector. The custom button is part of a annotationView. In CustomButton I put as property UIButtonType, but in the output nothing comes out. The output is a button with nothing, and when I get to touch up inside the annotation disappear while I want to open a view controller.
This is the code in CustomButton.h
@interface CustomButton : UIButton{
}
@property (nonatomic, strong)NSString * name;
@property (nonatomic, assign)UIButtonType typeButton;
@end
in CustomButton.m
@implementation CustomButton.h
@synthesize name;
@synthesize buttonType;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.typeButton = UIButtonTypeInfoLight;
}
return self;
}
in MapViewController.m
-(void)loadDetailListViewController: (CustomButton *)aName{
//I want to open a viewController
//.......
}
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id
<MKAnnotation>)annotation
{
//.......
MKPinAnnotationView *annotationView = (MKPinAnnotationView*) [mapView
dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier];
//.........
CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeCustom];
[rightButton setName:[NSString stringWithFormat:@"%@", self.nameTable]];
[rightButton addTarget:self action: @selector(loadDetailListViewController:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
return annotationView;
}
Why when I touch up inside the annotation disappear?
Please do follow the documentation instructions:
buttonWithType: Creates and returns a new button of the specified type.
- (id)buttonWithType:(UIButtonType)buttonType
Parameters
buttonType The button type. See UIButtonType for the possible values.
Return Value
A newly created button.
Discussion This method is a convenience constructor for creating button objects with specific configurations. It you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
Your code works somehow while you are using UIButtonTypeCustom
at buttonWithType:
, bad things will happen if someone change it (like to UIButtonTypeRoundedRect
).
As you are not using CustomButton -initWithFrame:
anywhere in the code but provided it's implementation, I can suggest you wanted to use it as the desired initializer which is fine.