我正在尝试使用自定义 segue 和可以在 UICollectionViewController 和 UITableViewController 之间切换的工具栏制作我自己的容器视图版本。
经过几次尝试,我让它工作了,它似乎表现得像它应该的那样,但后来我注意到我没有考虑适当地清理我的观点、儿童 VC 等。
我非常努力地遵循我所遵循的教程示例背后的逻辑(Ray Wenderlich's iOS by Tutorials 5 和 6),但我仍然认为我没有像使用 removeFromParentViewController 和 removeFromSuperview 消息那样释放内存。
为了更好地理解我的代码是如何工作的,这里有一个截图和一个简短的视频:
应用程序:
切换 VC 时的内存使用情况:
显示它的视频:
这是我处理视图层次结构和清理的代码部分(重写 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 时一样):
也许我并没有完全理解这些概念,但由于每次切换时内存都会增加,我觉得我的清理工作不正确,或者至少它遗漏了一些东西。
你能看到我错过了什么吗?