记录 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>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来不错,尽管您似乎对
-titleLabel
返回的内容有误解。-titleLabel
返回一个UILabel
实例 ->要检索UILabel
中包含的文本字符串,您必须调用-text
getter。This looks fine, although you seem to have a misconception about what
-titleLabel
returns.-titleLabel
returns aUILabel
instance -> To retrieve the text string contained within theUILabel
, you have to call the-text
getter.您一定在某个地方出现了双重释放错误,这意味着您的
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 (theCALayer
). Search forNSZombieEnabled
or justxcode zombies
and you should be able to turn on zombie detection which will give you more information.