I need to change the background of one of my Views based on a property that will be set when the View is initialized. Is this possible? I've got the background set as a UIImageView and if I set it to an Image in Interface Builder it works, but I can't change the background programatically.
Here is the code I'm using (I based it off a tutorial that loaded an image into a UITableCell):
+ (void) initialize {
/* The threat level images are cached as part of the class,
so they need to be explicitly retained.
*/
redLevel = [[UIImage imageNamed:@"red.png"] retain];
yellowLevel = [[UIImage imageNamed:@"yellow.png"] retain];
greenLevel = [[UIImage imageNamed:@"green.png"] retain];
}
-(id) initWithThreatLevel:(NSInteger) threatLevel {
if (self = [super initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]]) {
switch (threatLevel) {
case 1:
self.threat_levelImageView.image = greenLevel;
break;
case 2:
self.threat_levelImageView.image = yellowLevel;
break;
case 3:
self.threat_levelImageView.image = redLevel;
break;
default:
self.threat_levelImageView.image = greenLevel;
break;
}
}
return self;
}