2011-10-04 15 views
0

我试图获得UIButton的titleLabel字符串,但它记录为CALayer ...任何建议?记录UIButton titleLabel显示CALayer ....不是字符串

//ADD TWT BUTTON 
    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.titleLabel.hidden = YES; 
    twitter_share.titleLabel.alpha = 0; 
    twitter_share.tag = 20; 
    [twitter_share setTitle:@"test!" forState:UIControlStateNormal]; 

    UITapGestureRecognizer *tap_twt = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinButtonTap:)]; 
    tap_twt.numberOfTapsRequired = 1; 
    [twitter_share addGestureRecognizer:tap_twt]; 
    [tap_twt release]; 

    [annotationView addSubview:twitter_share]; 


- (void) handlePinButtonTap:(UITapGestureRecognizer *)gestureRecognizer { 
    UIButton *btn = (UIButton *) gestureRecognizer.view; 
    MKAnnotationView *av = (MKAnnotationView *)[btn superview]; 
    id<MKAnnotation> ann = av.annotation; 
    NSLog(@"handlePinButtonTap: ann.title=%@", ann.title); 

    NSString *testBtn = [NSString stringWithFormat:@"%@", [btn titleLabel]]; 

    NSLog(@"handlePinButtonTap: btn title=%@", testBtn); 
    } 

登录:

handlePinButtonTap: btn title=<UIButtonLabel: 0x70a14d0; frame = (0 3; 29 22); text = 'test!'; clipsToBounds = YES; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x70a6930>> 

回答

0

你必须有地方双重释放的错误,这意味着你的title串已经太早释放,它的内存已经被其它对象(CALayer) 。搜索NSZombieEnabled或只是xcode zombies,你应该能够打开僵尸检测,它会给你更多的信息。

+0

找到了!谢谢。我所要做的只是使用'btn.titleLabel.text'而不是'[btn titleLabel]' –

+2

这实际上并不是问题的正确答案。雅各布在发现这个简单的错误方面做得更好。 –

3

这看起来很好,尽管您似乎对-titleLabel返回的内容有误解。

-titleLabel返回UILabel实例 - >要检索包含UILabel文本字符串,你必须调用-text吸气。