本文介绍了addChildViewController实际上做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我刚刚第一次踏入iOS开发阶段,而我必须要做的第一件事就是实现一个自定义容器视图控制器 - 让我们称之为 SideBarViewController - 交换掉它显示的几个可能的子视图控制器中的哪一个,几乎完全像标准的 Tab Bar Controller 。 (它几乎是一个标签栏控制器,但有一个可隐藏的侧面菜单而不是标签栏。)I'm just dipping my feet for the first time into iOS development, and one of the first things I've had to do is implement a custom container view controller - lets call it SideBarViewController - that swaps out which of several possible child view controllers it shows, almost exactly like a standard Tab Bar Controller. (It's pretty much a Tab Bar Controller but with a hideable side menu instead of a tab bar.)根据Apple中的说明文档,每当我将一个子ViewController添加到我的容器时,我都会调用 addChildViewController 。用于交换当前子视图控制器的代码由 SideBarViewController 显示如下:As per the instructions in the Apple documentation, I call addChildViewController whenever I add a child ViewController to my container. My code for swapping out the current child view controller being shown by the SideBarViewController looks like this:- (void)showViewController:(UIViewController *)newViewController { UIViewController* oldViewController = [self.childViewControllers objectAtIndex:0]; [oldViewController removeFromParentViewController]; [oldViewController.view removeFromSuperview]; newViewController.view.frame = CGRectMake( 0, 0, self.view.frame.size.width, self.view.frame.size.height ); [self addChildViewController: newViewController]; [self.view addSubview: newViewController.view];}然后我开始试图找出 addChildViewController 在这里,我意识到我不知道。除了在 .childViewControllers 数组中粘贴新的 ViewController 之外,它似乎对任何东西都没有影响。从孩子控制器的视图到我在故事板上设置的子控制器的操作和出口仍然正常工作,即使我从来没有调用 addChildViewController ,我无法想象还有什么可能会影响。Then I started trying to figure out just what addChildViewController does here, and I realised that I have no idea. Besides sticking the new ViewController in the .childViewControllers array, it seems to have no effect on anything. Actions and outlets from the child controller's view to the child controller that I've set on the storyboard still work just fine even if I never call addChildViewController, and I can't imagine what else it could affect.的确,如果我重写我的代码而不是调用 addChildViewController ,而是看起来像这...Indeed, if I rewrite my code to not call addChildViewController, and instead look like this...- (void)showViewController:(UIViewController *)newViewController { // Get the current child from a member variable of `SideBarViewController` UIViewController* oldViewController = currentChildViewController; [oldViewController.view removeFromSuperview]; newViewController.view.frame = CGRectMake( 0, 0, self.view.frame.size.width, self.view.frame.size.height ); [self.view addSubview: newViewController.view]; currentChildViewController = newViewController;} ...然后我的应用程序仍能完美运行,据我所知!... then my app still works perfectly, so far as I can tell! Apple文档没有详细说明 addChildViewController 的作用,或为什么我们应该这样做叫它。有关该方法的相关描述的全部范围或其原因应在 UIViewController 类参考目前是:The Apple documentation doesn't shed much light on what addChildViewController does, or why we're supposed to call it. The entire extent of the relevant description of what the method does or why it should be used in its section in the UIViewController Class Reference is, at present:在同一页面上还有这个段落:There's also this paragraph earlier on the same page:以下是您可能需要调用的基本方法:Here are the essential methods you might need to call: addChildViewController: removeFromParentViewController willMoveToParentViewController: didMoveToParentViewController:addChildViewController: removeFromParentViewController willMoveToParentViewController: didMoveToParentViewController:但是它没有提供关于它所谈论的'事件'或'预期遏制行为'是什么,或者为什么(甚至何时)调用这些方法是必要的的任何线索。but it doesn't offer any clue as to what the 'events' or 'expected containment behavior' that it's talking about are, or why (or even when) calling these methods is 'essential'. Apple文档的自定义容器视图控制器部分中的自定义容器视图控制器的示例都调用此方法,因此我认为它有一些重要的用途除了将子ViewController弹出到一个数组之外,我无法弄清楚这个目的是什么。这个方法做了什么,为什么要调用它?The examples of custom container view controllers in the "Custom Container View Controllers" section of the Apple documentation all call this method, so I assume that it serves some important purpose beyond just popping the child ViewController onto an array, but I can't figure out what that purpose is. What does this method do, and why should I call it?推荐答案我也想知道这个问题。我观看了 WWDC 2011的会议102 视频和View Controller先生, Bruce D. Nilo ,说:I was wondering about this question too. I watched Session 102 of the WWDC 2011 videos and Mr. View Controller, Bruce D. Nilo, said this:所以似乎调用 addChildViewController:做得很少。呼叫的副作用是重要的部分。它们来自 parentViewController 和 childViewControllers 关系。以下是我所知道的一些副作用:So it seems that the call to addChildViewController: does very little. The side effects of the call are the important part. They come from the parentViewController and childViewControllers relationships. Here are some of the side effects that I know: 将外观方法转发到子视图控制器 转发轮换方法 (可能)转发内存警告 避免不一致的VC层次结构,特别是在 transitionFromViewController:toViewController:... 其中两个VC需要拥有相同的父级 允许自定义容器视图控制器参与状态保存和恢复 参与响应者链 连接 navigationController , tabBarController 等属性Forwarding appearance methods to child view controllersForwarding rotation methods(Possibly) forwarding memory warningsAvoiding inconsistent VC hierarchies, especially in transitionFromViewController:toViewController:… where both VCs need to have the same parentAllowing custom container view controllers to take part in State Preservation and RestorationTaking part in the responder chainHooking up the navigationController, tabBarController, etc properties 这篇关于addChildViewController实际上做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-01 06:53