gpt4 book ai didi

iphone - 实例化子 Controller

转载 作者:行者123 更新时间:2023-11-29 04:11:52 25 4
gpt4 key购买 nike

我有 1 个父 Controller 和 1 个 View Controller 。我已经成功执行了addChildViewControllertransitionFromViewController函数来利用 iPhone SDK 提供的 Containment API。然而,在我真正将 View Controller 传递给这些函数之前,我有几个问题

问题 1:在 ParentVC ViewDidLoad我必须使用以下方法实例化所有 4 个 subview Controller

UIViewController* child = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

self.myViewController = [[MyViewController alloc] init];

问题 2:假设我最初没有在我的 ViewDidLoad 中实例化所有 4 个 subview 。 ...每次我使用addChildViewController添加一个新的 child 在我使用 transitionFromViewController 执行转换之前,我是否需要实例化我想要添加的子 VC?

问题 3:在问题 1 和/或问题 2 的答案为"is"的情况下,必须对内存管理做什么。对于问题1,当从子VC导航回父VC时,这似乎意味着将创建4个 subview Controller 的新对象,对吗?当查看问题 2 时,当进行转换时(这种情况很常见),似乎每次都会创建 subview Controller 对象的新副本。从本质上讲,在父 subview 之间以及 subview 之间进行一些转换后,这个应用程序可能会变得非常臃肿。

问题 4:何时 transitionFromViewController被调用时,内存中发生了什么?在父子转换中,父 VC 会发生什么?在 child 到 child 的过渡过程中, child VC 会发生什么?

更新:以下引用来自 this particular tutorial似乎提到了我的一些担忧(强调):

Note that self.storyboard isn’t set from an init call. At the moment, there isn’t a great place to put this initialization code. One option is to override setStoryboard: and another is viewDidLoad. The problem with both options is that you should guard the addChildViewController: call to make sure that View Controllers aren’t instantiated twice.

最佳答案

  1. 不,如果您不愿意,则无​​需实例化自定义容器 View Controller 的 viewDidLoad 中的所有子级。如果你愿意的话可以,但不是必须的。如果您愿意,可以及时实例化它们。

  2. 您不仅需要在 transitionFromViewController 之前实例化,而且显然也需要在 addChildController 之前实例化。

  3. 如果您选择预先实例化所有 View Controller ,则各个 Controller 创建的成员对象取决于您在这些 Controller 中如何处理它,因此不可能抽象地回答这个问题。但现在这可能有点学术性了,因为您知道不必立即实例化所有潜在的子级。

  4. transitionFromViewController 不会影响 Controller 消耗的内存。哪些控件将是 Controller 的实例化(并将其添加到 addChildViewController)以及随后的 removeFromParentViewController(假设这是您对 Controller 的最后一个强引用)可以与 transitionFromViewController 结合使用。

关于iphone - 实例化子 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com