iphone - 子类化 UIViewController 会导致内存问题吗?

标签 iphone ios xcode

子类化 UIViewController 以防止“masterButtons”和“subButtons”集的代码重复。

这一切似乎都运行得很好,直到我意识到在大约 1200 个页面更改之后 - 仅在 3 个空白页面之间导航(仅显示按钮和一些其他对象)应用程序将总是崩溃!

仪器(+在设备上进行测试)没有显示任何内存泄漏,但确实显示了这些按钮对象的数百个实例!(选择了“已创建且仍然存在”过滤器。) 另外,viewDidUnload/didReceiveMemoryWarning 永远不会从任何 ViewController 中调用!

我在应用程序的其他页面上没有遇到这些问题(其中 UIViewController 没有子类化)。

因此看来 ViewController 的内容正在被重新创建,并且任何先前创建的内容都不会被删除。 子类化 UIViewController 是否存在可能导致此问题的常见陷阱?我可能会遗漏什么吗?

建议将不胜感激。 (遇到这个问题我真想把我的 Mac 扔出窗外!)

Top_ViewController      (contains 'MasterButtons')
       v
Area1_ViewController    (subclass of TOP_ViewController) (contains 'subButtons', and  a few texts fields etc.)
       v
aPage_ViewController    (subclass of Area1_ViewController)  (currently blank page)

(使用 ARC 和 Storyboard。 Storyboard View Controller 有一个各自的“aPage_ViewController”作为其类。)

最佳答案

如果您以循环方式创建 Storyboard,则可能会一直创建新的viewController。每个segue转换都会创建新的viewController,因此可能不会出现内存泄漏,但您的内存将会被消耗。

您应该使用 UITabBarControllerUINavigationController 来回移动,并且永远不要创建循环。

此外,viewDidUnload在iOS6中已被弃用,也许您忽略了警告?

编辑:

尝试这样的事情:

-(void)tab1_IsPressed:(UIButton *)paramSender{
    Top_ViewController *target_VC = (Top_ViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"page_1"];
    [self.navigationController popViewControllerAnimated:NO];
    [self.navigationController pushViewController:target_VC animated:NO];
}
-(void)tab2_IsPressed:(UIButton *)paramSender{
    Top_ViewController *target_VC = (Top_ViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"page_2"];
    [self.navigationController popViewControllerAnimated:NO];
    [self.navigationController pushViewController:target_VC animated:NO];
}

关于iphone - 子类化 UIViewController 会导致内存问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13052649/

相关文章:

ios - 我如何为 iOS 克隆/复制某种 UIView?

ios - 保存UITableView的用户输入数据

ios - 动态更改 CustomTableViewCell 上的图像

iphone - Objective-C 和 ASIHTTPRequest - 响应字符串问题

JavaScript 在移动设备上无法获得正确的窗口像素宽度

ios - Xcode 8.3.2 断点不起作用

ios - 这个函数 "override func ` self`() -> Self"的作用是什么?

ios - MapKit:路线未显示在两个注释之间

iphone - Cocos2D CCSprite 问题?

iphone - 非 ARC(自动引用计数)和 ARC 用户的开源