开发者

how to release UIVIewController after remove his view

How to release view controller created like this:

VCClass *vc = [[VCClass alloc] initWithNibName:@"VCClass" bundle:nil];
[self.view addSubview:vc.v开发者_如何学运维iew];

so the view appear, UIViewController is allocated. Now I want to releas it from within VCClass. I call inside VCClass:

[self.view removeFromSuperView];

my question is, where should I release "vc" object attached to removed view. Is there a good way to notify viewcontroller that is can be released while view is released ?


addSubview does a +1 to the retain count, and it's usually a good practice to release as soon as you don't need it, and you're handing it to another pointer. It's like a glass ball, it is passed hand by hand, and if no one is holding, it falls to the ground and breaks.

Example:

UIView *sampleView = [[UIView alloc] init]; // Retain count: 1
[self.view addSubview:sampleView];          // Retain count: 2
[self.view release];                        // Retain count: 1

When the removeFromSubview: is called, the object will be released:

[sampleView removeFromSuperView];           // Retain count: 0

That's for memory management.

Answering your question, a safer way to do what you want to do (loading just a part of an ViewController from a nib (I'm assuming you're using a nib, because you used @"VCClass" in the initWithNibName:), is to use it as following:

NSArray *nib =  [[NSBundle mainBundle] loadNibNamed:@"VCClass" owner:self options:nil];

UIView *view = (UIView*)[nib objectAtIndex:0];

This works by loading the NibName into memory, and then stealing the first element (if you only have a UIView inside, then it will pick that, as the top-most element). This is done similarly for UITableViewCells when loading them from nib files. Nib Files are autoreleased, and it makes more sense, since you apparently just care about the view itself, not the controller.


After you remove it, add a call to

[self autorelease];


Views don't know about their view controllers except as a weak reference to a delegate. This is to avoid a circular reference, among other reasons. VCs often have a life outside their views - hence, the viewDidLoad and viewDidUnload messages. For example, throughout the lifetime of a tab-bar application, the VCs for each tab might go through many different view instances while never being deallocated. Therefore, you should avoid having the view release its own view controller.

Often, the class that allocated the VC should be the one to release it. In the code you provided, you have:

VCClass *vc = [[VCClass alloc] initWithNibName:@"VCClass" bundle:nil];
[self.view addSubview:vc.view];

The controller class that the above code is in is probably the place best suited to releasing the VC. You might need to devise a delegate call just for this purpose.


[self.view removeFromSuperView]; should release your said view from the memory. Though be warned that this will not be true if your view has been retained by any other object that is its retain count is more than 1. Also look at the second answer on this thread.

Does UIView's removeFromSuperView method remove the UIView from memory

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜