ARC内存管理-无效什么? [英] Memory Management, ARC - what to nil?

查看:54
本文介绍了ARC内存管理-无效什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景-

我正在对项目使用自动引用计数.根视图是一个表视图(主/详细设置),显示幻灯片放映"列表.单击一个表格单元格,您将转到详细信息视图,该视图由一个包含视图(viewController.view)的Scroll视图组成(这是幻灯片放映").每个幻灯片都有一个封面和封底(相同的视图控制器,格式不同),其中夹有可变数量的页面.这是加载幻灯片的代码:

I am using automatic reference counting on a project. The root view is a Table View (Master / Detail setup) showing a list of "slide shows". Click on a table cell and you are taken to the detail view which consists of a Scroll view with views (viewController.view) in it (this is the "slide show"). Each slide show has a front cover and back cover (same view controller formatted differently) that sandwich an variable number of pages. Here is the code to load the slide show:

- (void)loadScrollView
{    
// The front and back cover are set in Interface Builder because they
// are reused for every slide show, just labels are changed.
[self.scrollView addSubview:self.frontCoverViewController.view];
[self.frontCoverViewController setCoverTitle:_data.name creationDate:_data.creationDate isFrontCover:YES];
[self.pagesArray addObject:self.frontCoverViewController];

for (int i = 0; i < [self getTotalNumberOfComps]; i++) 
{
    PageViewController *pageView = [[PageViewController alloc] init];
    pageView.data = [_compsArray objectAtIndex:i];

    [_scrollView addSubview:pageView.view];
    pageView.data.imgView = pageView.imageView;
    pageView.slideShowViewController = self;
    [_pagesArray addObject:pageView];
}

[self.scrollView addSubview:self.backCoverViewController.view];
[self.backCoverViewController setCoverTitle:_data.name creationDate:_data.creationDate isFrontCover:NO];
[self.pagesArray addObject:self.backCoverViewController];

[self.scrollView bringSubviewToFront:_frontCoverViewController.view];
[self setCurrentPage:0];
}

问题-

因此,我试图重用此幻灯片放映视图控制器,因此我需要在其中居中并重新创建页面,因为每个幻灯片均具有不同数量的幻灯片.请注意,幻灯片[PageViewController]只是其中包含ImageView的视图.它具有更多功能,因此我们需要控制器,但是V.C的主显示屏.是ImageView.我创建了以下方法来清空"幻灯片,然后再次使用新数据运行loadScrollView.这是空方法:

So Im trying to reuse this slide show view controller so I need to nil and recreate the pages in the middle because each slide show has a different number of slides. Note a slide [PageViewController] is just a view with an ImageView in it. It has more functionality so we need the controller however the main display of the V.C. is the ImageView. I have created the following method to "empty" the slide show before running loadScrollView again with new data. Here is the empty method:

- (void)saflyEmptyScrollView
{
for (int i = 0; i < [self.pagesArray count]; i++) 
{
    if (i == 0 && i == ([self.pagesArray count]-1)) {
        CoverViewController *cover = (CoverViewController*)[self.pagesArray objectAtIndex:i];
        [cover.view removeFromSuperview]; 
    } else {
        PageViewController *page = (PageViewController*)[self.pagesArray objectAtIndex:i];
        [page.view removeFromSuperview];
        page = nil;
    }
}
self.pagesArray = nil;
self.pagesArray = [[NSMutableArray alloc] init];
} 

大问题-

我的主要问题是我是否需要将每个页面的ImageView设置为nil?还是将页面本身设置为nil还可以释放该视图控制器中使用的ImageView/Labels/etc所占用的内存?

My main question is do I need to set the ImageView of each of these pages to nil? Or does setting the page itself to nil also free up the memory used by the ImageView/Labels/etc that are used in that view controller?

我尝试添加self.imageView = nil;到PageViewController的viewDidUnload和viewWillUnload方法(一次不同时使用),我意识到设置page = nil不会调用页面Unload方法.我可以正确释放内存吗?

I tried adding self.imageView = nil; to the PageViewController's viewDidUnload and viewWillUnload methods (one at a time not in both) and I realized that setting page = nil does not call the pages Unload methods. Am I freeing up memory correctly.

我已经阅读了很多文章,但是我仍然不确定我是否以最佳方式管理内存.非常感谢您的帮助!

I've read a lot of articles but Im still not sure if Im managing memory in the best way possible. Thanks so much for the help!

推荐答案

通常,您不必将其设置为nil.在这种特定情况下,将事情设置为nil无济于事.

Generally, you shouldn't have to set things to nil. And in this specific case, the setting things to nil is doing nothing.

page = nil;是多余的,因为无论如何变量page之后都立即超出范围. ARC知道这一点,不需要您将其设置为nil.

The line page = nil; is redundant, because the variable page goes out of scope immediately afterwards anyway. ARC knows this and doesn't need you to set it to nil.

self.pagesArray = nil;是多余的,因为您将其跟在self.pagesArray = [[NSMutableArray alloc] init];后面.第二行本身就足够了.

And self.pagesArray = nil; is redundant because you follow it with self.pagesArray = [[NSMutableArray alloc] init];. The second line on its own will suffice.

这篇关于ARC内存管理-无效什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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