Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 10-13-2008, 05:34 PM   #4 (permalink)
rames44
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

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.
rames44 is offline   Reply With Quote
 

» Advertisements
» Online Users: 696
23 members and 673 guests
306Designs, apatsufas, BrianSlick, chits12345, ckgni, coder, Dani77, devangvyas, Domele, ghost, JamesCahall, jameswilliards, k2c, my02820, nitingohel, Objective Zero, Oral B, Prajakta, rky, robin4kneb, skrew88, StormFactory, Vad
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,783
Threads: 89,204
Posts: 380,594
Top Poster: BrianSlick (7,129)
Welcome to our newest member, robin4kneb
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:58 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.