Ios 分段控制内存使用

Ios 分段控制内存使用,ios,memory-management,segue,uisegmentedcontrol,Ios,Memory Management,Segue,Uisegmentedcontrol,我正在尝试使用自定义segue和工具栏创建自己版本的容器视图,该工具栏可以在UICollectionViewController和UITableViewController之间切换 经过几次尝试,我让它工作起来,它似乎表现得像它应该的那样,但后来我注意到,我没有考虑对我的观点、儿童风投等进行适当的清理 我非常努力地遵循我所遵循的教程示例背后的逻辑(Ray Wenderlich的iOS by tutorials 5和6),但我仍然认为我没有像使用removeFromParentViewContro

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

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

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

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

应用程序:

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

显示它的视频:

下面是我处理视图层次结构和清理的代码部分(重写的perform:methodforthecustomsegue):

我还记录了父VC(第一个VC的选项卡栏)上的子项数:

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

也许我没有完全理解这些概念,但由于每次切换时记忆都会增加,我会觉得我的清理不正确,或者至少缺少了一些东西


你能看到我遗漏了什么吗

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

Hi Roma,知道这一点很好,但屏幕截图上也有泄漏。在这种情况下,我监控这两个指示器,只是因为仪器“泄漏”选项将两个指示器都放在了那里。谢谢泄漏模式为您提供了更多关于它们的信息。我不确定,但在“泄漏”模式下,您可以双击红色泄漏线,它将显示“问题部分”。也许你可以在分配模式下做同样的事情。
- (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];
}
NSLog(@"Amount of Children: %d", [self.childViewControllers count]);