In retrospect, I think I should have created a UIView within each XIN through interface builder and then a UIViewController for each of those. However, I skipped the whole model-view-controller thing and created m files that inherited from UIView. I directed the XIBs to inherit from my explicit UIViews.
This method worked perfectly fine until I wanted to dynamically load a background image into each of those UIViews. I didn't want to apply the images in interface builder because I had already placed all my controls and I didn't feel like removing them all. I turned to Google...
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];
I stripped off the "view" part because I'm writing a UIView rather than a UIViewController to get:
self.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];
I added this to the XCode generated method in each of my UIView files called "initWithFrame". The method has a section betwixt an if-statement where it directs you to place initialization code. Bingo.
But, it didn't work. Apparently, UIViews don't call initWithFrame upon initialization (or at all?); they call initWithCoder. The solution is to create a the initWithCoder method like so:
- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithCoder:coder]) {
self.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];
}
return self;
}
No comments:
Post a Comment