PageViewController:如何释放添加到其中的ViewControllers?
我对新的 PageViewController (具有漂亮的翻页动画的那个)有疑问。据我了解,有一堆 ViewController,您需要像这样设置:
PageView *startingViewController = [self.modelController viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
到目前为止一切顺利。然后您需要设置一个源(您的模型控制器)。在模型控制器中,您需要有四个方法:
-(PageView *)viewControllerAtIndex:(NSUInteger)index
-(NSUInteger)indexOfViewController:(PageView *)viewController
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
如果翻页(翻到下一页或上一页),则会调用最后两个方法。第二个只是确定页面索引号。有趣的是——也是我的问题所在——是第一个。第一个返回一个 ViewController(在我的示例中称为 PageView)。这是该方法的最后部分:
PageView *pView = [[PageView alloc] init];
return pView;
我想知道这个 pView 在哪里结束以及如何释放它?我想自动释放是一个坏主意,因为我不知道需要多长时间。如果它最终进入堆栈(我猜是这样),那么需要多长时间?当然只是接下来的几页。例如,想象一下为第 1 页设置一个 pView。然后您转到第 2 页和第 3 页。到那时您不再需要第 1 页 - 您可以释放它。如果您返回到第 1 页,它将重新加载。
我将日志命令放入 pView dealloc 中,但从未被调用。所以我想我泄漏了我创建的每一个视图控制器。
一旦不再需要它们,有什么想法如何以及在哪里释放它们吗?
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
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;
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
autorelease
正是您所需要的。这是autorelease
设计的完美情况,即您需要返回一个对象,但不知道需要多长时间。您的 PageView 实例是在堆(而不是堆栈)上分配的,PageViewController 将获得它的所有权,并在需要保留它时保留它。在您的方法返回后,它就成为 PageViewController 的责任。
(否则就使用 ARC 并让编译器处理它)
autorelease
is exactly what you need. This is the perfect situation for whichautorelease
was designed i.e. you need to return an object but don't know how long it will be needed.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.
(Otherwise just use ARC and let the compiler take care of it)