2011-10-04 54 views
0

我正在通過創建MKPinAnnotationView的子類創建自己的註釋標註。一切都很好,除了當我嘗試在新標註中添加按鈕時......按鈕從未被觸摸......任何想法爲什麼?繼承MKPinAnnotationView

在此先感謝!

代碼如下:

- (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]; 
    } 
+0

其實,要添加標籤和控件的註釋來看,不標註。標註是點擊註釋時彈出的氣泡。無論如何,嘗試[這個答案](http://stackoverflow.com/questions/6941199/how-to-get-click-event-from-a-button-added-over-mkannotationview/6942854#6942854)。 – Anna

+0

完美!這很好。你能否將它添加爲答案,以便我可以給你信用? –

回答

1

嘗試:

facebook_share = [UIButton buttonWithType:UIButtonTypeCustom]; 
facebook_share.frame = yourFrame; 
+0

什麼是'yourFrame'? –

+0

表示您要設置的框架.... – Maulik