使用ARC时是否应删除所有子视图

时间:2013-10-21 14:50:04

标签: ios6 uiview automatic-ref-counting

当用户在应用程序中移动时,我将UIViews堆叠在彼此之上,在不同屏幕之间前后移动。这些UIView可以是普通控件(按钮,标签等)以及从UIView继承的自定义控件。当按下其中一个屏幕上的后退按钮时,我会执行大量清理代码来释放实例变量的内存,以及停止计时器和关闭网络连接。我认为在这里使用ARC并部署到iOS 6和iOS 7设备也很重要。

典型的控件将按如下方式编码:

    UIImageView *ivSoundBottom = [[UIImageView alloc] initWithFrame:CGRectMake(270, 365, 30, 30)];
    UIImage *imgSoundBottom = [UIImage findCustomImage:@"ICN_Alarm_Sound_Bottom.png"];
    [ivSoundBottom setImage:imgSoundBottom];
    [self addSubview:ivSoundBottom];
    imgSoundBottom = nil;
    ivSoundBottom = nil;

此控件在屏幕加载时创建,永远不会再次引用。

我的问题是:在ARC下,我是否还需要遍历所有子视图并在每个子视图上调用removeFromSuperview来释放内存?

1 个答案:

答案 0 :(得分:1)

不,你没有。您也不需要这些陈述:

imgSoundBottom = nil;
ivSoundBottom = nil;

因为ARC会意识到对这些变量的引用超出了范围,并为你做了。