0

I'm creating my own annotation callout by subclassing MKPinAnnotationView. Everything works great, except for when I try to add buttons inside the new callout... the buttons are never called on touch... any idea why?

Thanks in advance!

Code is below:

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

  if (self) {
    self.frame = CGRectMake(0.0f, 0.0f, 284.0f, 245.0f);
    self.backgroundColor = [UIColor clearColor];

    UIImageView *callout_bkg = [[UIImageView alloc] init];
    callout_bkg.image = [UIImage imageNamed:@"callout_bkg.png"];
    callout_bkg.frame = CGRectMake(0, 0, 240, 110);
    [self addSubview:callout_bkg];

    titleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 8.0f, 145.0f, 20.0f)];
    titleLabel_.textColor       = [UIColor whiteColor];
    titleLabel_.textAlignment   = UITextAlignmentLeft;
    titleLabel_.backgroundColor = [UIColor clearColor];
    titleLabel_.font = [UIFont fontWithName:@"Geogrotesque" size:12];
    [self addSubview:titleLabel_];

    descLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 30.0f, 180.0f, 40.0f)];
    descLabel_.textColor       = [UIColor grayColor];
    descLabel_.textAlignment   = UITextAlignmentLeft;
    descLabel_.backgroundColor = [UIColor clearColor];
    descLabel_.numberOfLines = 15;
    descLabel_.font = [UIFont fontWithName:@"Geogrotesque" size:10];
    [self addSubview:descLabel_];

    communityLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(125.0f, 8.0f, 60.0f, 20.0f)];
    communityLabel_.textColor       = [UIColor whiteColor];
    communityLabel_.textAlignment   = UITextAlignmentRight;
    communityLabel_.backgroundColor = [UIColor clearColor];
    communityLabel_.font = [UIFont fontWithName:@"Geogrotesque" size:10];
    [self addSubview:communityLabel_];

    typeLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 72.0f, 50.0f, 20.0f)];
    typeLabel_.textColor       = [UIColor whiteColor];
    typeLabel_.textAlignment   = UITextAlignmentLeft;
    typeLabel_.backgroundColor = [UIColor clearColor];
    typeLabel_.font = [UIFont fontWithName:@"Geogrotesque" size:10];
    [self addSubview:typeLabel_];

    facebook_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 17, 29, 27)];
    facebook_share.backgroundColor = [UIColor clearColor];
    [facebook_share setBackgroundImage:[UIImage imageNamed:@"btn_annotation_share_fb.png"] forState:UIControlStateNormal];
    [facebook_share addTarget:self action:@selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
    [self addSubview:facebook_share];

    twitter_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 44, 29, 28)];
    twitter_share.backgroundColor = [UIColor clearColor];
    [twitter_share setBackgroundImage:[UIImage imageNamed:@"btn_annotation_share_twitter.png"] forState:UIControlStateNormal];
    [twitter_share addTarget:self action:@selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
    [self addSubview:twitter_share];
    }

    return self;
    }


-(void)calloutAccessoryTapped {
    NSLog(@"TEST!");
}


- (void)dealloc {
    [facebook_share release];
    [twitter_share release];

    [community_ release], community_ = nil;
    [communityLabel_ release], communityLabel_ = nil;

    [type_ release], type_ = nil;
    [typeLabel_ release], typeLabel_ = nil;

    [desc_ release], desc_ = nil;
    [descLabel_ release], descLabel_ = nil;

    [title_ release], title_ = nil;
    [titleLabel_ release], titleLabel_ = nil;

    [super dealloc];
    }

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    titleLabel_.text = self.type;
    descLabel_.text = self.desc;
    communityLabel_.text = self.community;
    typeLabel_.text = self.title;
    [facebook_share addTarget:self action:@selector(test:) forControlEvents:UIControlStateNormal];
    }
Adam Storr
  • 1,438
  • 2
  • 21
  • 46
  • Actually, you are adding labels and controls to the annotation view, not the callout. The callout is the bubble that pops up when you tap an annotation. Anyway, try [this answer](http://stackoverflow.com/questions/6941199/how-to-get-click-event-from-a-button-added-over-mkannotationview/6942854#6942854). –  Oct 04 '11 at 13:36
  • Perfect! That worked great. Could you add it as an answer so I can give you credit? – Adam Storr Oct 04 '11 at 18:42

1 Answers1

1

try with :

facebook_share = [UIButton buttonWithType:UIButtonTypeCustom];
facebook_share.frame = yourFrame;
Maulik
  • 19,348
  • 14
  • 82
  • 137