Arc:为多个按钮设置对nil的引用 [英] Arc: Setting references to nil, for multiple buttons

查看:74
本文介绍了Arc:为多个按钮设置对nil的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在ViewController中显示诸如按钮之类的项目时,我在头文件中声明它:

When displaying an item such as a button in a ViewController, I declare it in the header file:

@property (nonatomic, strong) UIButton *startButton;

在实施文件中综合它:

@synthesize startButton;

在实施文件中启动它:

startButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

并在viewDidUnload中将引用设置为nil:

And set the reference to nil in viewDidUnload:

[self setStartButton:nil];

然而,如果我有60个列出的项目(在UIScrollView中)这是在一个循环中动态创建的,在viewDidLoad中(从Core Data提供)?
(不使用TableView,因为这些项目非常复杂且具有交互性。)

However, what if I have 60 listed items (in a UIScrollView) which are created dynamically in a loop, in viewDidLoad (fed from Core Data)? (Not using TableView, because the items are quite complicated and interactive.)

我应该先在任何地方申报这些项目吗? 我应该把它们设置为nil吗?(或者我应该保持手指交叉并向Arc祈祷?)

Should I declare the items anywhere first? Should I set them to nil anywhere? (Or should I just keep my fingers crossed and pray to the Arc?)

一些指导是赞赏。谢谢。

Some guidance would be appreciated. Thanks.

推荐答案

您的视图控制器是否具有指向这60个项目中的每一个的属性或实例变量?

Does your view controller have properties or instance variables that point to each of those 60 items?

是:然后在 -viewDidUnload 中将这些ivars或属性设置为nil。

Yes: Then set those ivars or properties to nil in your -viewDidUnload.

否:然后没什么值得担心的。

No: Then there's nothing to worry about.

ARC更改你做了什么管理内存,但它不会改变基本原则,即每个对象都应该处理自己对其他对象的引用,但不要担心别人的对象。

ARC changes what you do to manage memory, but it doesn't change the basic philosophy, which is that every object should take care of its own references to other objects but not worry about anybody else's.

在这种情况下,包含所有这些项的视图将具有对项的强引用(通过它的子视图数组)。当视图控制器释放其视图时,该视图将被释放并在该过程中释放其所有子视图。因此,所有这些项目都将得到妥善处理。如果您确实对这些项中的任何项都有强引用,那么这些引用将阻止引用的对象被释放 - 将引用设置为nil将避免这种情况。

In this case, the view that contains all those items will have strong references to the items (via it's array of subviews). When the view controller releases its view, that view will then be deallocated and release all its subviews in the process. So all those items will be taken care of. If you did have strong references to any of those items, those references will prevent the referenced objects from being deallocated -- setting the references to nil will avoid that.

这篇关于Arc:为多个按钮设置对nil的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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