ios - 需要有关多个 subview Controller 的建议

标签 ios iphone objective-c cocoa-touch childviewcontroller

全部,

在我的 ParentViewController 中使用多个 ChildViewControllers 时,我遇到了一些性能/内存问题。这是我的情况:我有一个 ParentViewController,其中包含动态数量的 ChildViewControllers - 有时多达 20 个。它们包含在 UIScrollView 中, 并被分页。当我在页面上有几个时我遇到了问题(我只加载前两个,然后在我滑动时构建其他的),但是,在 ParentViewController 中有那么多开始由于内存导致一些崩溃。

ChildViewController 中有很多内容,我正在检查它以确保它尽可能高效,但是,我担心这种方法在旧设备上的工作(如是,我在 5S 上遇到了崩溃)。

将 View Controller 更改为 View 似乎会有所帮助,但由于 VC 很复杂,这将是一项相当大的工作。我的一个建议是从现有 View Controller 的 View 创建一个 View ,并在 View 上设置几个委托(delegate)方法,并以这种方式与 ParentViewController 的 View 进行交互。与当前使用 ChildViewControllers 的方法相比,是否有人对该方法的效率有任何想法?

我的另一个想法是构建一个自定义的 ContainerViewController 并让其中的所有子项都可以滑动,但我不确定这是否比在UIScrollView.

有什么想法吗?

最佳答案

我个人不提倡重构您的代码以使用 View 而不是 View Controller 。 View Controller 本身不太可能是内存问题的根源,而是它们跟踪的模型对象(以及 View Controller 的 View 使用的 Assets )。我认为关键是在 View Controller (及其 View )滚出屏幕时简单地删除它们。

在您的滚动逻辑中,当您添加滚动到 View 中的 subview Controller 时,您可能正在执行所有适当的包含调用:

UIViewController *newChildViewController = ...
[self addChildViewController:newChildViewController];
newChildViewController.view.frame = ...; 
[self.scrollView addSubview:newChildViewController.view];
[newChildViewController didMoveToParentViewController:self];

(请参阅 WWDC 2011 视频 Implementing UIViewController Containment 讨论为什么执行这些包含调用很重要,即保持 View Controller 层次结构与 View 层次结构同步。)

当 subview 滚出 View 时,您只需执行适当的包含调用即可删除子 Controller (及其 View ):

[childViewControllerToRemove willMoveToParentViewController:nil];
[childViewControllerToRemove.view removeFromSuperview];
[childViewControllerToRemove removeFromParentViewController];
// also remove any other strong references you have to that childViewControllerToRemove

或者,您可能想考虑使用 UIPageViewController它(在 iOS 6+ 中)为 transitionStyle 提供滚动页面 View (UIPageViewControllerTransitionStyleScroll)。这简化了您必须编写的自定义容器代码的数量,以处理滚动进出 View 的 View Controller 。 UIPageViewController 正是为这种在一堆不同 View Controller 的 View 中滚动(或分页)的情况而设计的。查看Page View Controllers iOS View Controller Catalog 中的讨论。

关于ios - 需要有关多个 subview Controller 的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21142193/

相关文章:

ios - CLLocationManager坐标

ios - 使用 dequeueReusableCellWithIdentifier 时保留单元格之间的单元格属性

ios - 在iOS swift/Objective C中实现Diffie Hellman生成公钥和私钥的方法有哪些?(除了Chilkat)

objective-c - 检查 NSRunLoop 并使用适当的 NSURLConnection 方法

ios - 如何在 ImageView ios 中添加图像

ios - Xcode 6.0.1 - 要么卡在启动屏幕 xib 上,要么只显示黑屏。错误不知从何而来

iphone - iPhone 上的 ACS 管理服务

iphone - Xcode 设备问题 - 即使设备未通过密码锁定,_shouldMakeReadyForDevelopment 检查也失败

iphone - @property @synthesize

ios - 在 Swift 中逐像素对图像应用视觉效果