PageViewController:如何释放添加到其中的 ViewController? [英] PageViewController: How to release ViewControllers added to it?

查看:21
本文介绍了PageViewController:如何释放添加到其中的 ViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对新的 PageViewController(具有漂亮的翻页动画的那个)有问题.据我了解,您需要像这样设置一堆 ViewController:

I have a problem with the new PageViewController (the one with the nifty page turn animations). As far as I understand, there is a stack of ViewControllers which you need to set up like so:

PageView *startingViewController = [self.modelController viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];

到目前为止一切顺利.然后你需要设置一个源(你的模型控制器).在你的模型控制器中,你需要有四个方法:

So far so good. Then you need to set up a source (your model Controller). In your model controller, you need to have four methods:

-(PageView *)viewControllerAtIndex:(NSUInteger)index
-(NSUInteger)indexOfViewController:(PageView *)viewController
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController

如果您翻页(翻到下一页或上一页),则调用最后两个.第二个只是确定页面索引号.有趣的——也是我的问题所在——是第一个.第一个返回一个 ViewController(在我的示例中称为 PageView).这是方法的最后:

The last two are called if you turn the page (to the next or the previous). The second one simply determines the page index number. The interesting one - and the one where my problem is - is the first one. The first one returns a ViewController (which in my example is called PageView). This is the very end of the method:

PageView *pView = [[PageView alloc] init];
return pView;

我想知道这个 pView 在哪里结束以及我如何发布它?我想 autorelease 是个坏主意,因为我不知道需要多长时间.如果它最终进入堆栈(我猜是这样),它需要多长时间?当然只是为了接下来的几页.例如,想象为第 1 页设置 pView.然后您转到第 2 页和第 3 页.届时您不再需要第 1 页 - 您可以释放它.如果您返回到第 1 页,它将被重新加载.

I am wondering where this pView ends up and how I can release it? I guess autorelease is a bad idea as I don't know how long it is needed. If it ends up in the stack (which I guess it does), how long is it needed? Surely just for the next couple of pages. For instance, imagine setting up a pView for page 1. You then turn to page 2 and 3. By then you don't need page 1 anymore - you could release it. If you go back to page 1 it will be reloaded.

我将日志命令放在我的 pView dealloc 中,但它从未被调用.所以我想我正在泄漏我创建的每个视图控制器.

I put log commands in my pView dealloc, but it is never called. So I guess I'm leaking every single viewControllers I've created.

是否有任何想法在不再需要它们时如何以及在何处释放它们?

Any ideas how and where to release them once they are not needed anymore?

推荐答案

autorelease正是您所需要的.这是设计 autorelease 的完美情况,即您需要返回一个对象,但不知道需要多长时间.

autorelease is exactly what you need. This is the perfect situation for which autorelease was designed i.e. you need to return an object but don't know how long it will be needed.

PageView *pView = [[PageView alloc] init] autorelease];
return pView;

您的 PageView 实例是在堆(而不是堆栈)上分配的,如果需要保留它,PageViewController 将获得它的所有权并保留它.在您的方法返回后,它成为 PageViewController 的责任.

Your PageView instance is allocated on the heap (not the stack) and PageViewController will take ownership of it and retain it if it needs to keep it around. It becomes PageViewController's responsibility after your method has returned.

(否则就用ARC,让编译器来处理)

(Otherwise just use ARC and let the compiler take care of it)

这篇关于PageViewController:如何释放添加到其中的 ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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