0

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?

Ortensia C.
  • 4,666
  • 11
  • 43
  • 70
  • If all you want to do is access some annotation-related info in the loadDetailListViewController method, you can just use a regular UIButton and use the calloutAccessoryControlTapped delegate method in which you can access view.annotation. –  Nov 08 '12 at 14:27
  • How do I? You please show me an example? – Ortensia C. Nov 08 '12 at 14:34
  • 1
    See http://stackoverflow.com/questions/4565197/how-to-find-which-annotation-send-showdetails and http://stackoverflow.com/questions/7334068/show-another-view-when-map-annotation-are-clicked. –  Nov 08 '12 at 14:50
  • @AnnaKarenina thanks a lot, now it all works! – Ortensia C. Nov 08 '12 at 15:21

1 Answers1

1

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.

A-Live
  • 8,904
  • 2
  • 39
  • 74