-1

我正在尝试使用自定义 segue 和可以在 UICollectionViewController 和 UITableViewController 之间切换的工具栏制作我自己的容器视图版本。

经过几次尝试,我让它工作了,它似乎表现得像它应该的那样,但后来我注意到我没有考虑适当地清理我的观点、儿童 VC 等。

我非常努力地遵循我所遵循的教程示例背后的逻辑(Ray Wenderlich's iOS by Tutorials 5 和 6),但我仍然认为我没有像使用 removeFromParentViewController 和 removeFromSuperview 消息那样释放内存。

为了更好地理解我的代码是如何工作的,这里有一个截图和一个简短的视频:

应用程序:

在此处输入图像描述

切换 VC 时的内存使用情况:

在此处输入图像描述

显示它的视频:

YouTube 链接

这是我处理视图层次结构和清理的代码部分(重写 perform: 自定义 segue 的方法):

- (void) perform
{
    // Set source and destination view controllers
    FirstViewController *sourceViewController = (FirstViewController *) self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;

    // Handle child and parent view controller designation (the view controller’s view is added to the window hierarchy)
    destinationViewController.view.frame = sourceViewController.containerView.bounds;

    [sourceViewController addChildViewController:destinationViewController];

    [destinationViewController.view removeFromSuperview];

    [sourceViewController.containerView addSubview:destinationViewController.view];

    [destinationViewController didMoveToParentViewController:sourceViewController];

    // Remove actual destinationViewController from the container every time there's a transition (segue)
    [destinationViewController removeFromParentViewController];
}

我还在父 VC(标签栏 First VC)上记录子节点的数量:

NSLog(@"Amount of Children: %d", [self.childViewControllers count]);

我正在检查计数是否增加(就像我忽略使用 removeFromParentViewController 时一样):

在此处输入图像描述

也许我并没有完全理解这些概念,但由于每次切换时内存都会增加,我觉得我的清理工作不正确,或者至少它遗漏了一些东西。

你能看到我错过了什么吗?

4

1 回答 1

1

分析时选择“泄漏”来检查内存泄漏。现在您正在检查内存分配,由于新对象的初始化,在视图切换之间内存使用量会增加是正常的。您应该避免的事情是在视图之间连续切换后内存不断增长

于 2013-06-27T15:23:20.580 回答