views:

296

answers:

1

I made a fresh view-based app project which has a MyProjectViewController.xib. Then I created AnotherViewController class with an acomanying XIB file. I opened MyProjectViewController.xib up in IB and dropped a "View Controller" object into my window. But now I want to hook the view of that view controllers up with the view of MyProjectViewController.xib. I see no way of doing that in IB. Must I do that programmatically?

The thing is, AnotherViewController will be created with an XIB right? I think I must hook it up somehow otherwise it's "lost in space". Hope you know what I mean...

+1  A: 

You can create a UIView derived class (.h and .m), call it AnotherView in Xcode and then create a view XIB (also in Xcode). Open the XIB in IB and change the view's class identity to be the name of your UIView derived class, AnotherView. Customize the UIView in IB to your hearts content and then you load it as a subview in your view controller with something like this:

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AnotherView" 
                                               owner:self options:nil];

// Will need to #import "AnotherView.h" for this to work
AnotherView *view = (AnotherView*)[array objectAtIndex:0];
// Size the view to whatever you need it to be
[view setFrame:viewRect];
// Add it to the view hierarchy of the view controller's view.
[[self view] addSubview:view];

Let me know if you need any clarification.

Matt Long
Hi Matt, thanks for the help. Actually I wanted to do everything in IB, not programmatically. My problem is, that I have configured the xib of the first view controller that comes with a view-based app template. in there, i want to put another view controller where it's view is a small subview of the main view. gonna try that with the self-made uiview subclass. maybe works.
openfrog
In that case you can just create another UIView in your view controller's XIB an customize it adding actions and outlets you connect in your top level view controller.
Matt Long