从UIButton获取UILabel [英] Get UILabel out of UIButton

查看:112
本文介绍了从UIButton获取UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIButton添加了UILabel作为子视图。

I have an UIButton to which an UILabel was added as a subview. Is there an easy way to get the UILabel back out of it so I can change it's title ?

推荐答案

如果你有一个简单的方法来取回UILabel,你仍然有一个标签,你可以通过搜索标签来查找它$ c>。

If you assign a tag to it while you still have a reference to it, you can later find it by searching for views with that tag.

像这样:

UILabel *label = [[UILabel alloc] init...];
label.tag = 1000;

后来...

UILabel *label = (UILabel *)[button viewWithTag:1000];

如果您无法设置标记,您还可以循环按钮的子视图,查找 UILabel 的实例:

If there's no way for you to set the tag, you can also loop over the button's subviews, looking for an instance of UILabel:

UILabel *label;
for (NSObject *view in button.subviews) {
    if ([view isKindOfClass:[UILabel class]]) {
        label = (UILabel *)view;
        break;
    }
}
// Do stuff with label

这篇关于从UIButton获取UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆