I have a ViewController that includes its own .nib file, I am wanting to load another .nib into this ViewController that will be a menu of sorts… however I am just not sure how to do this and was hoping for some help.
Before I show you the code I will explaine what I have done.
- I have a UIViewController with a Nib that is my main view.
- I have created another nib which is going to be a menu that appears as a subview to the main view
- I have changed this nibs class to the UIViewController of the main view so that it can see all of the same IBActions and outlets.
-
I have then run the code below trying to load the seperate nib as a subview however when I run it the subview of the other nib is not showing up….
jumpBarPortraitNib = [[UIView alloc] initWithFrame:CGRectMake(0.0, 50.0, 320.0, 367.0)]; // add jumpbar container to view [self.view insertSubview:jumpBarPortraitNib belowSubview:actionTabBar];
There’s clearly some confusion here.
A nib is merely an archive that contains(*) objects, including (usually) one or more view objects (
UIViewin your case). You don’t “load a nib as a subview.” You instantiate the nib, potentially handing it to aUIViewControllerto own and manage it, and add a view referenced within the nib to your view hierarchy.All you have done with your first line of code is create a generic
UIViewinstance with a given frame size and insert it into whatever [self view] is. There is nothing in what you wrote that actually references a nib, even though you misnamed your view as a nib.For what you are doing, I would probably go one of two routes:
IBOutlet. You can then display the menu easily from within the commonUIViewControllerinstance you have set as the nib’s File Owner.UIViewControllerinstance that will just manage the menu view, and which you will use to instantiate the nib’s contents using-initWithNibName:as @Aaron mentioned. (Note that you shouldn’t need to set the frame externally; the frame will be archived in the nib as a property of the view.)*: Not really, but you can think of it this way.