简体   繁体   中英

How iterate inside for in UIView elements

I have this code but i cant iterate inside a UIScroll for my main UIView (self) elements

how iterate for all elements in a main UIView

 for (UIView *objet in self.view.subviews) {

        if ([objet isKindOfClass:[UIButton class]]) {

             [objet setBackgroundColor:[UIColor blueColor]];

        }

    }

And I've also tried this but does not work.

for (UIView *objeto in super.self.view.subviews) { 

    if ([objeto isKindOfClass:[UIButton class]]) {

         [objeto setBackgroundColor:[UIColor blueColor]];

        }

 }

Try this:

for (UIView *objet in self.view.subviews) {

    if ([objet isKindOfClass:[UIButton class]]) {
        UIButton *aButton = (UIButton *)objet;

        [aButton setBackgroundColor:[UIColor blueColor]];

    }

}

The compiler will probably be much happier with you (and you won't be seeing warnings about calling " setBackgroundColor " on a UIView).

How you creating the subviews? You can setTag: for each, and then use something like:

for (int i = 0; i < subviewsCount; ++i)
{
    [[superview viewWithTag:i] setBackgroundColor: [UIColor blueColor]];
}

Other way - put all subviews into NSMutableArray. Then you can use:

for (UIView *currentView in arrayOfSubviews)
{
    [currentView setBackgroundColor: [UIColor blueColor]];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM