I've setup a nib file to include some basic UIView elements within a larger UIView element. Two elements are subclasses of the UIImageView class (meters to display data to the user). Anyway, in my mind I'd like to have these meters be their own nib file as they have a number of visual elements and for sanity sake it would be nice to place the meter individually in its own nib file. I would also like to have one controller for the meter and the other basic elements in the parent nib file. I've set the child meter nib objects in the parent nib to my custom class (say UIMeter). I'm having trouble loading the child nib into the parent nib, when I override the UIMeter initWithCoder method it creates an infinite loop as i'm trying to use loadnibnamed to reach into the child nib file for loading. I've also tried loading the nib from the parent controller but it loads both the child nib and the nib from the loadnibnamed. This will probably make more sense with code and images:
UIMeter code, not really certain that I even need this as it causes an infinite loop but I'm hoping that the view can be smart enough to load itself in some way though in reading the docs it seems more appropriate to do this in the controller for the view.
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
self = [[[NSBundle mainBundle] loadNibNamed:@"PowerMeterView" owner:self options:nil] firstObject];
}
return self;
}
In the parent controller I'm dialing up the view like so:
-(void)loadView
{
if(![self isViewLoaded])
{
GameMenuView *menuView = [GameMenuView new];
menuView.userEnergy = [[[NSBundle mainBundle] loadNibNamed:@"PowerMeterView" owner:self options:nil] firstObject];
menuView.userReactor = [[[NSBundle mainBundle] loadNibNamed:@"PowerMeterView" owner:self options:nil] firstObject];
_gameMenuView = menuView;
//--This places the child nib from the file on the view but it is not replacing the UIMeter in the parent nib
[menuView.userEnergy setFrame:CGRectMake(0, 0, 20, 20)];
[menuView addSubview:menuView.userEnergy];
[self setView:menuView];
}
}