gpt4 book ai didi

iOS 内存泄漏, View Controller 的自定义 TabBar 失控分配

转载 作者:行者123 更新时间:2023-11-29 12:56:56 26 4
gpt4 key购买 nike

我正在使用以下代码在两个选项卡之间切换:

-(void)switchPage:(UIButton *)sender{


DLog(@"");
DLog(@"child view controllers %lu", (unsigned long)self.childViewControllers.count);


switch(sender.tag){
case 0:{

for(id object in self.childViewControllers){

if([object isKindOfClass:[QuestionsVC class]]){


[questionsTab willMoveToParentViewController:nil];
[questionsTab.view removeFromSuperview];
[questionsTab removeFromParentViewController];
questionsTab = nil;

}

}

questionsTab = [[QuestionsVC alloc] init];
[self addChildViewController:questionsTab];
[self.view addSubview: questionsTab.view];
[questionsTab didMoveToParentViewController:self];
[self.view bringSubviewToFront:tabBarView];
[self drawGrayLineLayer];

break;
}

case 1:{


for(id object in self.childViewControllers){

if([object isKindOfClass:[AnswersVC class]]){

[answersTab willMoveToParentViewController:nil];
[answersTab.view removeFromSuperview];
[answersTab removeFromParentViewController];
answersTab = nil;

}

}

answersTab = [[AnswersVC alloc] init];
[self addChildViewController:answersTab];
[self.view addSubview: answersTab.view];
[answersTab didMoveToParentViewController:self];
[self.view bringSubviewToFront:tabBarView];
[self drawGrayLineLayer];

break;
}

}

奇怪的是, Root View Controller (选项卡栏)的子 VC 计数保持不变,而没有 subview Controller 被释放。

问题是什么?

下面是一个仪器快照,告诉我每次代码运行时每个 View Controller 都会重新分配,而旧的不会被释放。

enter image description here enter image description here

最佳答案

我遇到的问题是我从 subview Controller 中初始化了其他对象,这些对象随后具有委托(delegate)属性,并且该委托(delegate)是 strong 因此对象保留了其父对象,即委托(delegate)—— viewcontroller,它创建了保留循环。

根据我所学,代表应该几乎总是(或者几乎从不)

关于iOS 内存泄漏, View Controller 的自定义 TabBar 失控分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20832674/

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