记录 UIButton titleLabel 显示 CALayer...而不是字符串

发布于 2024-12-07 21:57:18 字数 1539 浏览 0 评论 0 原文

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

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>>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-12-14 21:57:18

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

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

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.

内心旳酸楚 2024-12-14 21:57:18

您一定在某个地方出现了双重释放错误,这意味着您的 title 字符串已被过早释放,并且其内存已被另一个对象(CALayer)占用。搜索 NSZombieEnabled 或仅搜索 xcode僵尸,您应该能够打开僵尸检测,这将为您提供更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文