ztabxq
Last Updated: February 25, 2016
·
875
· anthonylevings
Speechrocket4 square

Manipulating a view controller's unnamed and untagged views in Xcode (iOS)

First we want to build an array of the subviews:

NSArray *subviews = [self.view subviews];

Next we add a sequentially numbered picture to each view using a for-in statement:

int i = 0;
for (UIView* v in subviews) {
           UIImageView *picture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d%@", @"picture",i,@".jpg"]]];
        picture.bounds = v.bounds;
        [v addSubview:picture];
       i++;};

This of course assumes we have as many images as there are subviews, and we would most likely want in a real-world situation to check that the image exists at the file location before adding it to the UIImageView.

More detail can be found here: http://sketchytech.blogspot.co.uk/2012/04/view-with-no-name-xcode-forin-statement.html

Say Thanks
Respond