gpt4 book ai didi

ios - push viewcontroller 是一个实例变量,pop 时不会调用 dealloc 方法

转载 作者:可可西里 更新时间:2023-11-01 05:43:39 25 4
gpt4 key购买 nike

我有一个 AViewController,如果我像这样将 BViewController 创建为实例变量

@interface AViewController ()
{
BViewController *bVC;
}
@end

然后推

- (void)push {
bVC = [[BViewController alloc] init];
[self.navigationController pushViewController:bVC animated:YES];
}

当 BViewController 弹出时,不会调用 Dealloc 方法。

两个 View Controller 几乎都是空的,我的意思是没有 NSTimers、 block 或网络请求。我猜想 AViewController 中存在一个强引用循环,但无法弄清楚循环。

所以,谁能帮我解决这个问题,谢谢!

最佳答案

这是基本的内存管理101。

只有在不再有对该对象的强引用后,该对象才会被释放。

您的 ivar bVC 对 View Controller 有很强的引用。插入它会增加另一个。然后弹出它,它又返回到 bVC 的唯一强引用。所以它不会被释放。

您可以避免使用 ivar 或在某个时候将其设置为 nil,以便可以释放 View Controller 。

关于ios - push viewcontroller 是一个实例变量,pop 时不会调用 dealloc 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32262973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com