Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 773
jlstrecker Avatar asked Feb 11 '11 06:02

jlstrecker


People also ask

How do I connect my class to Xib?

Here's a more step-by-step way to associate your new UIViewController and . xib. Select File's Owner under Placeholders on the left pane of IB. In the Property Inspector(right pane of IB), select the third tab and edit "Class" under "Custom Class" to be the name of your new UIViewController subclass.

How do you call a Xib file?

To get an object from a xib file programatically you can use: [[NSBundle mainBundle] loadNibNamed:@"MyXibName" owner:self options:nil] which returns an array of the top level objects in the xib.


1 Answers

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.

like image 71
Jonah Avatar answered Sep 18 '22 12:09

Jonah