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 ?
Asked
Active
Viewed 906 times
1 Answers
3
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

Douwe Maan
- 6,888
- 2
- 34
- 35
-
Thanks for your answer but is there any other way in case you don't have a reference to it? – Oysio Sep 18 '10 at 15:39