0

I have a grid on screen. It is an instance of a UIView subclass. In cells in the grid I want to add instances of subviews - another custom subclass of UIView - as the user interacts with the app. The subview itself is something I want to design in an XIB. Its controls represent state for that particular cell. It consists of a couple of UIControls (say a label and a button).

I already know I can load NIBs dynamically using

[[NSBundle mainBundle] loadNibNamed:@"MyNIB" owner:self options:nil];

and I know they'll work if I set the File's Owner property in the NIB to the view controller that's responsible for the view these subviews will be added to. I use this to add some external NIBs to a UIScrollview in the main app screen.

What I want to know is how can I do this dynamically? I want to say something like:

MySubviewCell * sv = [[NSBundle mainBundle] loadNibNamed:@"MyNIB" owner:self options:nil];
[sv setFoo:@"Foo"];
[sv setBar:123];
[sv setFrame:myrect];
[mainView addSubview:sv];

But of course loadNibNamed doesn't return me the subview instance, instead it returns an array of all the controls in the view.

EDIT File's Owner in the MySubviewCell NIB will be a problem: there isn't one, and there can't be: I don't know how many I'll need. Should I be using something like an ArrayController?

EDIT 2 Please ignore the previous edit; I've left it in as the answer refers to it but it's not the problem I thought it was.

Can I do this?

Thanks

Tim
  • 5,024
  • 2
  • 30
  • 58
  • Files owner should only be a problem if you are loading the nib with several different owning classes, unless I am not understanding you. – jrturton Nov 26 '11 at 18:03

2 Answers2

4

This is commonly used when designing table view cells in a nib. You've got two options:

  1. The object at index 0 of the returned array will be the "root" object in your nib.
  2. Create an outlet in your view controller of type MySubviewCell. Set your file's owner class in the nib to your view controller, and link the outlet to the element in the nib you are interested in. When you load the nib with owner self, the outlet becomes populated. You can then configure it, add it to your array, and set the outlet back to nil to prepare to load the nib again for the next instance.
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Element zero. Thanks! That worked well. For point (2), I found that I could set File's Owner in MySubviewCell to MySubviewCell. I added outlets for each control in the subview, but I connected them to the View object in IB, *not* to the File's Owner, even though they were the same class. If I connected through File's Owner, it crashed. – Tim Nov 26 '11 at 18:21
  • Files owner has to be the class of the object loading the nib, if you are sending self as owner. If not, send nil as owner. – jrturton Nov 26 '11 at 18:32
1

Try to access to first object in that array:

MySubviewCell * sv = (MySubviewCell*)[[[NSBundle mainBundle] loadNibNamed:@"MyNIB" 
                                                                    owner:self 
                                                                  options:nil]
                                       objectAtIndex:0];
beryllium
  • 29,669
  • 15
  • 106
  • 125