Quote:
|
So, when I create something and add it to the view, I have to release it, and the view that holds it adds it's own reference ?
|
Exactly.
With very, very few exceptions, any time you say
Code:
anObject.property = object;
the code for "property" retains the object.
Basically, any class that wants an object to persist retains it itself, just in case nobody else wanted to hold onto it. Thus, for example, you could create a button, add it as a subview of your view and then release it if you didn't need to manipulate it any further. The view retains it when you add the button to the view, so the button doesn't get destroyed when you release it, but the only reference to the object would be that of the view, and when the view got released, it would then release the button and the button would thus get destroyed. Very elegant. On the other hand, if you wanted to hold onto the button yourself, you would typically do:
Code:
UIButton *button = [[UIButton alloc] init...];
self.myButton = button;
[myView addSubview:button];
[button release];
Assuming that you defined
Code:
@property (nonatomic, retain) UIButton *myButton;
then the "self.myButton = button" line will add a reference (balanced by the "release" that you would code into your "dealloc" routine) because of the nature of the code that the compiler generates for you in the "@synthesize" line, the view has one, and the explicit release still balances out your "alloc".
Hope that makes it clear.