I'm aware best practices for Objective-C development says IBOutlets should always be defined as properties according to Apple.
From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong.
But for learning purposes let's say we got the following scenario using ARC:
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController{
IBOutlet UIButton *buttonA;
IBOutlet UIButton *buttonB;
IBOutlet UIButton *buttonC;
}
@end
If I'm not mistaken, those three buttons are strong, so my question is: Will those buttons be released from memory once the ViewController is released?
Those buttons would be released automatically if they were weak, I know that, but not sure if they are strong.
Can anyone please help? Just to be clear, the method 'dealloc' on DetailViewController is empty.
You asked:
Yes. Or, more accurately, those buttons will be deallocated when there are no more strong references. And in this scenario, those buttons now have two
strongreferences, one being the view controller and another being the view to which these buttons were added as subviews. Both of thosestrongreferences would need to be relieved before the button would be deallocated.But why would you want to maintain two strong references to that control? Generally you let the view maintain the
strongreference to its subviews (i.e. let the view "own" its subviews), and the view controller is only using aweakreference to those subviews.When the view controller has the
weakreference, the buttons are being released because the onlystrongreference to the button is maintained by its superview and when that view is removed, then the button will lose its last strong reference and can be deallocated.If the view controller has a
strongreference, you are unnecessarily added anotherstrongreference that needs to be relieved before the buttons are deallocated. You can do that, but it's unnecessary. You quoted from the Resource Programming Guide, but the preceding sentence says "you don’t need strong references to objects lower down in the graph because they're owned by their parents, and you should minimize the risk of creating strong reference cycles."Bottom line, your example with the implicitly
strongreferences to theIBOutletcontrols will work fine. But there's no advantage to having the view controller maintain astrongreference to the buttons and it represents a bit of a misunderstanding of the object graph. View controllers really should only be maintainingweakreferences to the controls on their views.