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