iPhone how to add a view controller's view to

2019-03-22 02:46发布

This has been long on my mind, and I do not really know how to properly add a view that's managed by a view controller to another view controller's view.

This does not work, because the view does not finish loading

self.messageViewController = [[PopupMessagesViewController alloc] initWithNibName:@"PopupMessagesViewController" bundle:nil];
[self.view addSubview:self.messageViewController.view];

How can I add a UIView that a view controller creates from a nib to another view controller's view? How can I force such view to load before adding it?

3条回答
放荡不羁爱自由
2楼-- · 2019-03-22 02:58

Try this

- (void)viewWillAppear: (BOOL)animated {
    [super viewWillAppear: animated];
    [self.messageViewController viewWillAppear];
}

- (void)viewDidAppear: (BOOL)animated {
    [super viewDidAppear: animated];
    [self.messageViewController viewDidAppear];
}
查看更多
叛逆
3楼-- · 2019-03-22 03:12

You need to create a Container View Controller. While iOS 5 explicitly supports container controllers, you can create container controllers in previous versions. All iOS 5 does is do some automatic forwarding of rotation/appearance events (optional...and personally I find them annoying, sending the events before I'm ready) and give you some extra methods to use in your implementation. The real issue in creating a Container View Controller is sending all the appropriate events to the sub-controllers and making sure you manage your controllers in a way that is consistent with Apple's implementation. Otherwise, you'll get odd behavior in your sub-controllers. You really need to make sure you fully understand how view controllers work in their entirety before you do this. I recommend reading the following:

Here's some links to info: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html -Scroll down to: Implementing a Container View Controller

Also here for the view controller life cycle, which will help you figure out which calls need to be made in which order: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW1

I do recommend reading the entire View Controller Programming Guide....you can gleam a lot of information from there: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457-CH1-SW1

查看更多
时光不老,我们不散
4楼-- · 2019-03-22 03:14

In general, don't do that. You're breaking some of the assumptions about how UIViewControllers will be used and it is likely to cause you problems in the future. You're not going to be able to count on the subview's controller receiving all of the UIViewController lifecycle method calls you might expect.

Valid solutions are to use the iOS 5 container view controller methods to add the subview's controller as a child view controller or to have a non-UIViewController controller class responsible for managing that subview if you need to encapsulate that behavior.

查看更多
登录 后发表回答