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)

AppFusion - 6 in 1!
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-28-2010, 04:35 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 84
Default removeFromSuperview releases subviews?

When you remove a view from its superview using "removeFromSuperview" does it remove the view's subview as well or do you have to remove the subviews yourself before removing the view from its superview?

Thanks.
Shubo is offline   Reply With Quote
Old 10-28-2010, 04:39 PM   #2 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Default

Quote:
Originally Posted by Shubo View Post
When you remove a view from its superview using "removeFromSuperview" does it remove the view's subview as well or do you have to remove the subviews yourself before removing the view from its superview?

Thanks.
If you have a view, that has subviews, from my experience removing just the View also removes the subviews.

With questions like these, I usually create a simple program isolating the problem and try different things to see what the outcome is.
Not_Appy_At_All is offline   Reply With Quote
Old 10-28-2010, 04:46 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 84
Default

Thanks. That's what I thought but my app is getting bigger slightly over a time so I was wondering the subviews weren't being released.
Shubo is offline   Reply With Quote
Old 10-28-2010, 04:55 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,931
Default

Quote:
Originally Posted by Shubo View Post
When you remove a view from its superview using "removeFromSuperview" does it remove the view's subview as well or do you have to remove the subviews yourself before removing the view from its superview?

Thanks.
The answer is "it depends."

The view you remove from it's superview won't remove ITs views, but it might release them. Here's how it works.

If you remove a view from it's superview, the superview sends the newly removed view a release message. If that causes it's retain count to drop to zero, it gets deallocated. When a view is deallocated, it releases it's subviews also.

If you want to have a view (and all it's subviews) get deallocated when you remove it from it from it's superview, make sure the only object retaining it is it's superview. Then, when you remove it, the whole ball of wax gets deallocated.

If you want the view and it's subviews to stick around so you can use it somewhere else, have your view controller save it in a retained property.

This is a very common design pattern in Cocoa. You have some object that serves as a container for other objects. (views, arrays, dictionaries, sets, and custom data container objects, and even complex groups like an array of dictionaries of arrays of objects) Only the container object retains the things it contains. When you release the container object, everything "inside" the container gets released.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-28-2010, 05:02 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 84
Default

Thank for clarifying. I see a possible problem. I am retaining the view so even though I remove it from its superview it and its subviews might not get released.

Quote:
Originally Posted by Duncan C View Post
The answer is "it depends."

The view you remove from it's superview won't remove ITs views, but it might release them. Here's how it works.

If you remove a view from it's superview, the superview sends the newly removed view a release message. If that causes it's retain count to drop to zero, it gets deallocated. When a view is deallocated, it releases it's subviews also.

If you want to have a view (and all it's subviews) get deallocated when you remove it from it from it's superview, make sure the only object retaining it is it's superview. Then, when you remove it, the whole ball of wax gets deallocated.

If you want the view and it's subviews to stick around so you can use it somewhere else, have your view controller save it in a retained property.

This is a very common design pattern in Cocoa. You have some object that serves as a container for other objects. (views, arrays, dictionaries, sets, and custom data container objects, and even complex groups like an array of dictionaries of arrays of objects) Only the container object retains the things it contains. When you release the container object, everything "inside" the container gets released.
Shubo is offline   Reply With Quote
Old 10-28-2010, 06:54 PM   #6 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Default

Quote:
Originally Posted by Shubo View Post
Thank for clarifying. I see a possible problem. I am retaining the view so even though I remove it from its superview it and its subviews might not get released.

I never retain any view or its subviews.

I remove them and reinitialize them when I need them...

I think the term is "Lazy Loading"?

I also release the View and all its subviews as soon as I create them...

So when I remove the view from superview, it removes all of it...

But Duncan's post makes me think I need to do some more reading and playing around under the hood...
Not_Appy_At_All is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 613
21 members and 592 guests
ADY, bandley, BrianSlick, Creativ, dacapo, Dattee, djbrooks111, gbenna, HDshot, IphoneSdk, iseff, jakerocheleau, jbro, joeallenpro, linkmx, mer10, nimesh_158, Reyna, ukneeq, yurikus
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 160,619
Threads: 89,833
Posts: 383,490
Top Poster: BrianSlick (7,244)
Welcome to our newest member, yurikus
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:45 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0