UIPageViewController内存泄漏 [英] UIPageViewController memory leak

查看:238
本文介绍了UIPageViewController内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎UIPageViewController永远持有初始内容视图控制器. 例如:

DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;

startingViewController 永远不会释放,直到pageViewController本身释放.

要重现此错误,只需使用基于页面的应用程序模板在XCode中创建一个新项目即可.并将3行代码添加到DataViewController.m

@property NSInteger debugIndex; // file scope
NSLog(@"DataViewController[%d] created", self.debugIndex); // in viewDidLoad
NSLog(@"DataViewController[%d] dealloc", self.debugIndex); // in dealloc

当您沿垂直方向滚动演示应用程序时,将获得如下日志:

DataViewController[0] created  
DataViewController[1] created  
DataViewController[2] created  
DataViewController[1] dealloc  
DataViewController[3] created  
DataViewController[2] dealloc  
DataViewController[4] created  
DataViewController[3] dealloc  
DataViewController[5] created  
DataViewController[4] dealloc  
DataViewController[6] created  
DataViewController[5] dealloc  

DataViewController [0] 从未释放.

对此有何想法? 谢谢!

解决方案

您是否正在使用transitionStyle UIPageViewControllerTransitionStyleScroll?我遇到了相同或相似的问题,当改用页面卷曲动画时,该问题似乎消失了.

这个问题对我来说更加复杂,因为我允许UISliderBar设置内容中的位置.因此,在更改UISliderBar时,我正在调用setViewControllers:direction:animated:completion:,这导致越来越多的视图控制器引用被卡在"我的UIPageViewController中.

我也在使用ARC.我没有找到一种可接受的方式来强制UIPageViewController释放多余的视图控制器引用.我可能会要么使用页面卷曲过渡,要么使用启用了页面调度的UIScrollView来实现自己的UIPageViewController等效项,这样我就可以管理自己的视图控制器缓存,而不必依赖UIPageViewController损坏的视图控制器管理.

It seems that UIPageViewController is holding the initial content view controller forever. For example:

DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;

The startingViewController is never released until the pageViewController itself it released.

To reproduce this bug, just create a new project in XCode using the Page-Based Application template. And add 3 lines of code into DataViewController.m

@property NSInteger debugIndex; // file scope
NSLog(@"DataViewController[%d] created", self.debugIndex); // in viewDidLoad
NSLog(@"DataViewController[%d] dealloc", self.debugIndex); // in dealloc

And when you scroll the demo App in vertical orientation, you'll get logs like this:

DataViewController[0] created  
DataViewController[1] created  
DataViewController[2] created  
DataViewController[1] dealloc  
DataViewController[3] created  
DataViewController[2] dealloc  
DataViewController[4] created  
DataViewController[3] dealloc  
DataViewController[5] created  
DataViewController[4] dealloc  
DataViewController[6] created  
DataViewController[5] dealloc  

DataViewController[0] is never deallocated.

Any ideas about this? Thanks!

解决方案

Are you using transitionStyle UIPageViewControllerTransitionStyleScroll? I encountered the same or a similar problem which seemed to disappear when using page curl animations instead.

The problem was compounded for me because I was allowing a UISliderBar to set the position in the content. So on change of the UISliderBar, I was calling setViewControllers:direction:animated:completion: which caused more and more view controller references to get "stuck" in my UIPageViewController.

I am also using ARC. I have not found an acceptable way to force the UIPageViewController to let go of the extra view controller references. I will probably either end up using the page curl transition or implementing my own UIPageViewController equivalent using a UIScrollView with paging enabled so I can manage my own view controller cache instead of relying on UIPageViewController's broken view controller management.

这篇关于UIPageViewController内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆