jlstrecker jlstrecker - 3 months ago 16
iOS Question

How to embed a UIViewController's view from one xib inside a view in another xib?

MyViewController.xib has File's Owner class set to

MyViewController
(a subclass of
UIViewController
) and File's Owner view connected to a
UIView
containing some subviews.

OtherViewController.xib has File's Owner class set to
UIViewController
and File's Owner view connected to an empty
UIView
.

Is it possible in Interface Builder to embed
MyViewController
's view inside the view in OtherViewController.xib?

I tried adding an instance of
MyViewController
into OtherViewController.xib, but I can't drop it inside the view (because it's not a
UIView
) and I can't get to the view that was associated with
MyViewController
in MyViewController.xib (only the view controller itself, and nothing it's connected to, makes it over to OtherViewController.xib).

Answer

You probably do not want to do this. Follow the warning in the View Controller Programming Guide:

Note: If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. Then use a single view controller object to manage the generic controller objects.

A UIViewController subclass whose view does not fill the window will not behave as you might expect. It will not receive view controller lifecycle messages, rotation messages, or have its parentView/navigation/tabBarController properties set correctly.

A UITableViewCell should not be the view for a UIViewController. It might have some controller object responsible for managing its behavior (though I suspect this behavior can probably all be contained within the cell view itself) but that controller should not inherit from UIViewController.