When displaying an item such as a button in a ViewController, I declare it in the header file:
@property (nonatomic, strong) UIButton *startButton;
Synthesize it in the implementation file:
@synthesize startButton;
Initiate it in the implementation file:
startButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
And set the reference to nil in viewDidUnload:
[self setStartButton:nil];
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.)
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.
Does your view controller have properties or instance variables that point to each of those 60 items?
Yes: Then set those ivars or properties to nil in your
-viewDidUnload.No: Then there’s nothing to worry about.
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.
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.