user2738882
user2738882

Reputation: 1190

Get labels in current view

I have a need to get all labels in current view. is it possible? If yes, please advise how can i realize this?

For example, i need to collect all labels from startup screen, then from currently showing popup and etc.

Thanks!

Upvotes: 1

Views: 89

Answers (2)

Sudershan shastri
Sudershan shastri

Reputation: 720

Suppose if you want to collect label in an array, you can try this:

for (UIView *subview in self.view.subviews)
{
    if(subview isKindOfClass:[UILabel class])
    {
        [arrayOfLabels addObject:subview];
    }
}

If you want it to do by means of accessibilityLabel, here are the steps to get view from given accessibilityLabel. Method viewContainingAccessibilityElement:element is extension method to UIAccessibilityElement class.

UIAccessibilityElement *element = [[UIApplication sharedApplication] accessibilityElementWithLabel:label];
UIView *view = (UIView*)[UIAccessibilityElement viewContainingAccessibilityElement:element];

Let me know if it works

Upvotes: 0

user3228871
user3228871

Reputation: 103

Here is the way to go----

NSMutableArray *labels = [NSMutableArray array];
for (UIView *v in someSuperview.subviews) {
if ([v isKindOfClass:[UILabel class]]) {
    [labels addObject:v];
   }
}

Upvotes: 2

Related Questions