jlstrecker
jlstrecker

Reputation: 5033

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

Upvotes: 6

Views: 6405

Answers (4)

James Moore
James Moore

Reputation: 9027

This has changed since some of the other answers were posted - you want to take a look at the latest documentation for UIViewController, particularly the guide section "Presenting View Controllers from Other View Controllers" and the class reference guide section "Implementing a Container View Controller". Also, there's a video from WWDC 2012 covering the topic on iTunes: Session 236 - The Evolution of View Controllers on iOS. (The video is very useful, it's not just a general overview.)

Upvotes: 4

Jonah
Jonah

Reputation: 17958

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.

Upvotes: 7

Nathan
Nathan

Reputation: 190

This can be done programmaticly by adding a reference in OtherViewController to MyViewController. This is perhaps a bit messy and does in some way lead me to ask why you would want to do this but... I will trust that you know what you're doing.

Warning. Because 'Other' will contain a reference to 'My' you will want retain My inside Other. but Do not, I repeat do not retain 'Other' inside of 'My' this kind of cycle will lead to errors.

Good luck and don't forget to vote

ps if you have a little more detail I may be able to help you sort out a better design so that this sort of thing can be avoided :)

Upvotes: 0

TomSwift
TomSwift

Reputation: 39512

You can put it all in one xib. For example, just put it all in your MainWindow.xib.

Upvotes: 0

Related Questions