Get UILabel out of UIButton

非 Y 不嫁゛ 提交于 2019-12-12 19:16:17

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/3742138/get-uilabel-out-of-uibutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!