开发者

Get UILabel out of UIButton

开发者 https://www.devze.com 2023-01-17 03:55 出处:网络
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 c开发者_如何学Gohange it\'s title ?If you assign a tag to it while you st

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 c开发者_如何学Gohange it's title ?


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.

Like this:

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

Later...

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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消