views:

98

answers:

1

If I have a UIView, and I add an allocated subview into it (in this case the UIImageView), when I release the UIView, will UIImageView also be released?

I set up my view and a subview like this:

UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UIImage *myImage=[UIImage imageNamed:@"image.png"];

// This is the subview:
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image=myImage;

[view addSubview:imageView];
[imageView release];

Now, when I call:

[view removeFromSuperview];
[view release];

Will the allocated imageView also be released, or should I remove it by removing all of the view's subviews?

Any insight on this topic is greatly appreciated.

+4  A: 

Yes. The UIView "owns" the subviews so it releases them when it is itself dealloc'd.

Bill
... which isn't to say the subviews are themselves dealloc'd, since they may be retained by something else (or several somethings else).
Joshua Nozzi