How to embed a UIViewController's view from on

2019-04-28 06:15发布

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

4条回答
唯我独甜
2楼-- · 2019-04-28 06:36

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

查看更多
ら.Afraid
3楼-- · 2019-04-28 06:39

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.

查看更多
欢心
4楼-- · 2019-04-28 06:45

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

查看更多
姐就是有狂的资本
5楼-- · 2019-04-28 06:55

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

查看更多
登录 后发表回答