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
Written by Anthony Levings
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ios
Authors
Related Tags
#ios
#xcode
#uiimageview
#loop
#ios 5
#nsarray
#uiview
#for
#for...in
#if...else
#subviews
#xcode 4.3
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#