0

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];
    
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

2 Answers2

3

There's clearly some confusion here.

A nib is merely an archive that contains(*) objects, including (usually) one or more view objects (UIView in your case). You don't "load a nib as a subview." You instantiate the nib, potentially handing it to a UIViewController to 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 UIView instance 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:

  1. Add the view as a separate top-level view in your original nib, and connect it to an IBOutlet. You can then display the menu easily from within the common UIViewController instance you have set as the nib's File Owner.
  2. Have a separate UIViewController instance 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.

Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
  • okay from what you have said I am trying to achieve what you have outlined in option 1.. I have set up a IBOutlet UIView and done the right click drag to the menu view (in Interface builder) but its not displaying with that code.. what do you think I have missed? – HurkNburkS Jul 02 '12 at 22:48
  • Good answer. If you're just getting started with this type of programming you should read [this article](http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html) which helped me out a ton with the basic concepts when I was starting. – Aaron Brager Jul 02 '12 at 22:48
  • @HurkNburkS OK, suppose you named your property/outlet "menuView." Then you could do something as simple as `[[self view] addSubview:[self menuView]]`. – Conrad Shultz Jul 02 '12 at 22:53
  • just realized **Add the view as a separate top-level view in your original nib,** so I am now taking the IB stuff I have done in the other nib and am adding it to the mainViewController nib.. will see how it goes. – HurkNburkS Jul 02 '12 at 22:54
  • okay thanks Conrad just saw your second response am about to give it ago. – HurkNburkS Jul 02 '12 at 22:54
  • weird.. not sure why but when I took out the ** jumpBarPortraitNib = [[UIView alloc] init];** of that new view it started showing ;) – HurkNburkS Jul 02 '12 at 23:05
  • @HurkNburkS Was your outlet called jumpBarPortraitNib by any chance? – Conrad Shultz Jul 02 '12 at 23:06
  • @HurkNburkS It's a bit complicated, but you probably did a plain `@synthesize` for it, which creates a backing ivar of the same name. Then when you instantiated the generic `UIView` with that name, you clobbered the instance-scoped ivar of the same name with a locally declared copy. Removing that line fixed the ivar scope. But you shouldn't use ivars directly - that's why I wrote `[self menuView]` instead of just `menuView`. – Conrad Shultz Jul 02 '12 at 23:15
  • right I get you, How should have I handled the @synthesize then? – HurkNburkS Jul 03 '12 at 02:09
  • 1
    @HurkNburkS Well, don't call the outlet a nib (because it isn't), and use something like `@synthesize menuView = _menuView` - and make sure to use the accessor method `[self menuView]` instead of direct access. – Conrad Shultz Jul 03 '12 at 03:12
0

You need to use initWithNibName, and then set the frame on the following line:

UIViewController * jumpVC = [[UIViewController alloc] initWithNibName:@"jumpBarPortrait" bundle:nil];
jumpVC.view.frame = //your frame
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Or, if you subclassed `UIViewController`, replace that with your custom class (like `MyAwesomeViewController`). – Aaron Brager Jul 02 '12 at 22:43
  • but the other nib dose not have a UIViewController.... this is what I am abit confused by... – HurkNburkS Jul 02 '12 at 22:45
  • @HurkNburkS You can optionally create another UIViewController subclass to manage your menu. This has some theoretical advantages in the form of modularization. Please see my response. – Conrad Shultz Jul 02 '12 at 22:46
  • Every View has a View Controller. That's how you tell it what to do when you press a button, etc. – Aaron Brager Jul 02 '12 at 22:47