2

I have a view controller with generic interface elements that needs to load in a menu view. I have created a custom UIView called MenuView.h and MenuView.m and built all the views in it using code. However, I'd much rather set out the views in a .xib file so that I can lay them out more easily.

I do know about the whole loadNibNamed in order to load a nib/xib file into my custom UIView, however what I don't understand is how I can wire up some IBOutlets to the loaded nib.

Does anyone have any pointers to how this is done please?

Rakesh
  • 3,370
  • 2
  • 23
  • 41
jowie
  • 8,028
  • 8
  • 55
  • 94
  • I don't understand. If you are creating the view's in xib and using a custom uiviewcontroller where are you facing the problem in creating the iboutlets? – Rakesh Mar 12 '13 at 14:53
  • 1
    I'm not talking about using UIViewController. Say I wanted to create a custom UIView and load it into the view controller multiple times. I don't want to build all the elements within that UIView in code, I'd rather use a xib file, with outlets. I've heard of Custom Container View Controllers. Would that be where to look? – jowie Mar 12 '13 at 23:41
  • 1
    You can do that. Just specify the owner in the `loadNibNameNamed:owner:option` method as the object of your class that you have wired up your `IBOutlet`. – Rakesh Mar 13 '13 at 08:55
  • I'll post an answer with the two ways I know of doing this. – Rakesh Mar 13 '13 at 09:24
  • @jowie I'm curious as in say you have a custom view cView. Now you want to create the cView with a nib file and some `IBOutlet`s so that you can load it with `loadNibNamed` method and call the IBOutlets from whichever the view controller it is added in as a subview? – Zen Mar 13 '13 at 09:36
  • The ultimate would be to be able to just `alloc init` cView and it would handle all the nib-loading itself, so it just appeared on the screen :) – jowie Mar 13 '13 at 12:26

1 Answers1

3

To do this:

  1. Create xib.
  2. Set the file's owner as the class you want to act as a controller (usually a UIViewContoller subclass -although before iOs5, Apple doesn't recommend using UIViewController when having multiple controllers on screen). (This step is to make this class visible in the xib so that you can create your outlets).
  3. Create the IBOutlet/IBAction for your custom view in this class.
  4. And when calling loadNibNamed:owner:option: , give the owner as an object for this class.

i.e

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"CustomXib" owner:<customClassobject> option:nil] objectAtIndex:0];

and to use it somewhere:

[someView addSubview:view]

Another way would be to create a custom view controller and use the view controller to access the view .

  • Repeat steps 1-3 as above keeping the custom view controller(say MyViewCon) as the file's owner.You will also have to connect the view reference of the controller to the xib view.

Then instead of using loadNibNamed... , initialize the view controller. i.e

MyViewCon *myVC = [[MyViewCon alloc] initWithNibName:@"CustomXib"];

then wherever you have to use the view use myVC.view. For eg,

 [someview addSubview: myVC.view];

UPDATE:

If you are using the same UIViewController subclass as the file's owner for the child and parent view, you should realize that it will be two different objects of the same class and might cause confusion since the same variable might show different values when performing actions in the same scene.

For more information from a design point of view regarding multiple controllers for a scene refer here.

Community
  • 1
  • 1
Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • Just a small addition here. When you create your xib make sure you have only 1 parent view which contains all your other views. This way when you get the 0th element in the nib array, you will get the parent one. – Petar Mar 13 '13 at 09:40
  • Yup. Thats the usual case, i guess. – Rakesh Mar 13 '13 at 09:49
  • Point 2: How do I set the file's owner on an empty xib? And what if I want to use the custom view in multiple view controllers? I don't want the custom view or its xib tightly coupled with any view controller... – jowie Mar 13 '13 at 12:09
  • I selected the File's Owner and set its Custom Class on the right to the name of the associated class file. I take it that's the correct way to do it. I can now create outlets in the class and they are reflected in the xib. – jowie Mar 13 '13 at 12:14
  • I could just create a view controller, as you state at the end, but should I really be loading multiple view controllers within a full-screen view controller? I always thought that was a big no-no... – jowie Mar 13 '13 at 12:28
  • 1.You need some place to write code for your IBOutlets, which for our convenience we chose as the file's owner. If you feel that is tight coupling there is no other go. 2. And yes, thats the right way of setting the file's owner object's class. – Rakesh Mar 13 '13 at 14:43
  • I personally feel splitting up the logic in controller makes things more modularized. I don't know of any reason (doesn't mean there isn't) why we shouldn't use multiple view controllers for representing a particular scene . Anyway if you feel so, you could always make the file's owner of the component view as the same as that of the parent view. I have updated the answer to reflect a problem you might face in such a case. – Rakesh Mar 13 '13 at 14:48