记录UIButton titleLabel显示CALayer .... not string

时间:2011-10-04 17:00:48

标签: iphone uibutton title mkannotation ibaction

我正在尝试获取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>>

2 个答案:

答案 0 :(得分:3)

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

-titleLabel返回UILabel个实例 - &gt;要检索UILabel中包含的文本字符串,您必须调用-text getter。

答案 1 :(得分:0)

您必须在某处发生双重释放错误,这意味着您的title字符串过早被释放,其内存已被另一个对象(CALayer)占用。搜索NSZombieEnabledxcode zombies,您应该可以打开僵尸检测,这将为您提供更多信息。