3

I created some UIImageViews inside a nested UIView by Storyboard and I created a different TAG for each UIImageView. This is how the tree of the ViewController looks according to Storyboard:

  • ViewController
    • View
      • ViewNested
        • UIImageView1
        • UIImageView1
        • UIImageView1
        • UIImageView1

I have to change programmatically these ImageViews so, to get the images I use the method viewWithTag but it doesn't work because it returns NIL. This happens even if I add to my class the ViewNested IBOutlet and getting the views using the following code:

// View is the top View with Tag:40            
UIView * view = [self.view viewWithTag:40]; //This works
// The nestedView with Tag:44          
UIView * viewNested = [view viewWithTag:44]; //DOESN'T work it returns NIL even if the TAG is exact

Then if I try to access to the imageView using the same method of course, it returns NIL. I don't know why, I also tried to use this code to view all the recursive nested view but it seems that they don't exist even if they are present in the storyboard.

- (void)showAllSubView:(UIView *)view
{   int i = 0;
    for (UIView * subView in [view subviews]) {
        NSLog(@"%@, tag:%ld", subView, (long)subView.tag) ;
        [self showAllSubView:subView] ;
        i++;
    }
    NSLog(@"Numb of Views %d", i) ;

}

The TAGS are 40 for the root View, 44 for the nested and for the images are 1,2,3,4. So the TAGS are all different.

Any help will be appreciate :). Thanks in advance

Cris
  • 55
  • 1
  • 6
  • Update your question and show what the tag is for each view in your hierarchy. – rmaddy Apr 30 '15 at 18:13
  • 1
    Why wan't use IBOutlet? – OMGHaveFun Apr 30 '15 at 18:27
  • If `showAllSubView` says they are not subviews of `viewNested`, what makes you think that they should be? – Phillip Mills Apr 30 '15 at 19:14
  • To see what's really going on, insert a break-point somewhere in your code where you're sure all the views are loaded, then run this in the debug area console: `po [self.view recursiveDescription]`. It will print the complete view hierarchy, tags included. – Artal Apr 30 '15 at 20:07
  • @OMGHaveFun Because I have 18 UIImageViews so I would to access them by TAG not by IBOutlet – Cris Apr 30 '15 at 20:18
  • Are you sure viewNested is not nil when you call viewWithTag? – Tapani Apr 30 '15 at 20:21
  • @Artal I call the viewWithTag method not in the viewDidLoad..my question is why I cannot intercept the views that exist in the Storyboard, I really don't understand this. – Cris Apr 30 '15 at 20:23
  • @Tapani I checked that the nestedView is NIL, only the root isn't! I will edit the question – Cris Apr 30 '15 at 20:26
  • make sure you added tag for each view in the interface builder. – Aneeq Anwar Apr 30 '15 at 20:54
  • @Cris Ok, you can use IBOutletCollection, example: http://stackoverflow.com/questions/15836930/how-can-i-use-iboutletcollection-to-connect-multiple-uiimageviews-to-the-same-ou – OMGHaveFun Apr 30 '15 at 20:54
  • @OMGHaveFun Ok I see this data structure I will test this solution thank you! Do you have any idea about my issue, in your opinion is a normal behaviour of the nested views or there are some mistakes in my methods? – Cris Apr 30 '15 at 22:55
  • @Cris UIImageView *imageView=(UIImageView *)[self.view viewWithTag:yourTag]; [imageView setImage:[UIImage imageNamed:@"yourImageName"]]; – OMGHaveFun May 01 '15 at 20:48

1 Answers1

0
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:yourTag];
[imageView setImage:[UIImage imageNamed:@"yourImageName"]];

replace yourTag with UIImageView tag; replace yourImageName with some Image name

and if you want change only images of ImageViews - you don't need

UIView * view = [self.view viewWithTag:40]; //This works
// The nestedView with Tag:44          
UIView * viewNested = [view viewWithTag:44];

Also you can change all images:

for (int i=0; i<18; i++)
    {
            UIImageView *imageView=(UIImageView *)[self.view viewWithTag:i];
NSString *imageName = [NSString stringWithFormat:@"imageName_%d", i];
    [imageView setImage:[UIImage imageNamed:imageName]];
    }
OMGHaveFun
  • 838
  • 1
  • 10
  • 16