PageViewController:如何释放ViewControllers添加到它? [英] PageViewController: How to release ViewControllers added to it?
问题描述
我有一个新的PageViewController(一个有漂亮的页面转动画)的问题。据我所知,有一堆ViewControllers,你需要设置如下:
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,但它从来不调用。所以我想我泄露了我创建的每一个viewControllers。
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将获取它的所有权,保留它,如果它需要保持它。
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:如何释放ViewControllers添加到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!