iPhone如何将视图控制器的视图添加到另一个视图控制器的视图? [英] iPhone how to add a view controller's view to another view controller's view?
问题描述
这一直是我的想法,我真的不知道如何正确地将视图控制器管理的视图添加到另一个视图控制器的视图。
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];
如何将视图控制器从笔尖创建的UIView添加到另一个视图控制器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?
推荐答案
您需要创建一个容器视图控制器。虽然iOS 5明确支持容器控制器,但您可以在以前的版本中创建容器控制器。所有iOS 5都会做一些旋转/外观事件的自动转发(可选......我个人觉得它们很烦人,在我准备好之前发送事件)并给你一些额外的方法来实现。创建容器视图控制器的真正问题是将所有相应的事件发送到子控制器,并确保以与Apple实现一致的方式管理控制器。否则,您的子控制器会出现奇怪的行为。在执行此操作之前,您确实需要确保完全了解视图控制器的完整工作方式。我建议阅读以下内容:
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:
以下是一些信息链接: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html - 向下滚动到:实现容器视图控制器
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
此处还有视图控制器生命周期,它将帮助您确定需要以哪种顺序进行哪些调用:< a href =http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW1\"rel =nofollow> http ://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW1
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
我建议您阅读整个View Controller Pro编程指南....你可以从那里看到很多信息: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457 -CH1-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
这篇关于iPhone如何将视图控制器的视图添加到另一个视图控制器的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!