views:

4848

answers:

3

Hello All,

What event is fired when a view is removed from its super view? Do its sub-views receive any message? For example, I have subview2 and subview3 added to subview1 as in

super_view -> subview1 -> subview2 -> subview3

if I remove subview1 e.g. by [code] [subview1 removeFromSuperview]; [/code]

What event its subviews(subview2 and subview3) receive?

Is there a way to let subviews know that their super view is removed?

Thanx for helping, JinBaba

A: 

I don't think that subviews(2,3) will receive any event when subview1 is removed per se (at least nothing is mentioned in documentation).

EDIT:

Thinking about it more ... I believe that the subviews(2,3) will not receive event per se when the subview1 is released.

But as a side effect of subview1 getting released if subview1 is not retained somewhere else its ref count will reach 0 and it will get deallocated. During deallocation subview1 will release all its subviews.

In that case they will get released, I'm not sure if this is what you are after.

See Jane's answer.

stefanB
The documentation for removeFromSuperView says, "The receiver is also released;".Does not releasing a view releases its subviews?
MAbbas
+3  A: 

It depends on the retain count of subview2 and subview3. If you create them via [[UIView alloc] initWithFrame:frame], and then add them as subviews, they will have a retain count of 2. (Or 3, if you keep a reference in a retained property, i.e. self.subview2 = [[...

So if you want them to be released when subview1 is released, then ensure you give them another release after adding them as a subview, so that their retain count is just the single one from being added as a subview. Something like this...

UIView* subview2 = [[UIView alloc] initWithFrame:myFrame];
[subview1 addSubview:subview2];
[subview2 release];
Jane Sales
stefanB
Well Jane,As you mentioned, I am releasing the subview after adding it to a super view.But when I remove the super view say "subview1" by[self removeFromSuperView]; // Removing subview1 from its superviewdealloc for subview2 is not called.The documentation for "removeFromSuperView" mentions that callin it will release it also.Am I missing something?Thanx,
MAbbas
You must have an extra retain on subview2 - without seeing the code that's all I can say, sorry.And an up vote for my life? I like it, though I'm sure I should return the point or something :-)
Jane Sales
A: 

when a view is removed from its superview, all its child view is also removed from it resulting in reduction of retaincout by one.

Look at the following code snipet:

randomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oldbg.png"]];
randomImage.frame = CGRectMake(10, 10, 20, 20);

aview = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 200)];

NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);

[aview addSubview:randomImage];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[randomImage release];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);


[self.view addSubview:aview];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);
[aview release];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);

[aview removeFromSuperview];
NSLog(@"aview retain=%d,image retain=%d",[aview retainCount],[randomImage retainCount]);

And the console log is like this:

 2009-08-09 23:29:42.512 ActionSheetTest[744:20b] aview retain=1,image retain=1
2009-08-09 23:29:42.513 ActionSheetTest[744:20b] aview retain=1,image retain=2
2009-08-09 23:29:42.515 ActionSheetTest[744:20b] aview retain=1,image retain=1
2009-08-09 23:29:42.516 ActionSheetTest[744:20b] aview retain=2,image retain=1
2009-08-09 23:29:42.517 ActionSheetTest[744:20b] aview retain=1,image retain=1

actually at the last NSLog the app will crash, since both of the objets has retainCount =0.

Hope this helps.

Shaikh Sonny Aman