简体   繁体   中英

Logging UIButton titleLabel displays CALayer… not string

I'm trying to get the titleLabel string of a UIButton, but it's logging as a CALayer... any suggestions?

//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);
    }

Log:

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

This looks fine, although you seem to have a misconception about what -titleLabel returns.

-titleLabel returns a UILabel instance -> To retrieve the text string contained within the UILabel , you have to call the -text getter.

You must have a double-release error somewhere, which means your title string has been deallocated too soon, and its memory has been taken by another object (the CALayer ). Search for NSZombieEnabled or just xcode zombies and you should be able to turn on zombie detection which will give you more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM