I have a view in a xib which contains UIImageView. I’m setting the image to be displayed in the xib and it loads and displays correctly.
However in some circumstances I want the loaded image to be replaced with another image at runtime, so I am doing this:
NSArray* viewObjects = [[NSBundle mainBundle] loadNibNamed:@"ParentView"owner:self options:nil];
id object = [viewObjects objectAtIndex:0];
parentView *parentView = (ParentView*)[viewObjects objectAtIndex:0];
UIImage *image = [UIImage imageNamed: @"iconName"];
UIImageView *iv = [[UIImageView alloc] initWithImage: image];
iv.frame = parentView.icon.frame;
parentView.icon = iv;
parentView.icon is a UIImageView. The image variable is not nil, nor is iv as the code executes.
iconName is a .png called “iconName@2x.png” which is in my project and if I set it as the UIImageView within the .xib then it is loaded and displayed correctly. But if I set another icon in the .xib and attempt to replace it at runtime with the code above then its not getting replaced.
I can’t figure out why this isn’t working.
You are creating a new image view but don’t add it to the view hierarchy. Either use the existing image view and assign a new
UIImageto it or add the newly created view to view hierarchy usingaddSubview.