删除视图后如何释放 UIVIewController

发布于 2024-12-01 22:57:00 字数 350 浏览 2 评论 0原文

如何释放像这样创建的视图控制器:

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

因此视图出现,UIViewController 被分配。现在我想从 VCClass 中释放它。我在 VCClass 内部调用:

[self.view removeFromSuperView];

我的问题是,我应该在哪里释放附加到已删除视图的“vc”对象。有没有一种好方法可以在视图释放时通知视图控制器可以释放?

How to release view controller created like this:

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-12-08 22:57:00

addSubview 对保留计数执行+1,通常一个好的做法是在不需要它时立即释放它,并将其交给另一个指针。它就像一个玻璃球,一手一手地传递着,如果没有人握住,它就会掉到地上摔碎。

示例:

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

当调用removeFromSubview:时,该对象将被释放:

[sampleView removeFromSuperView];           // Retain count: 0

这用于内存管理。

回答你的问题,一种更安全的方法来做你想做的事情(从笔尖加载 ViewController 的一部分(我假设你正在使用笔尖,因为你使用了 @"VCClass"initWithNibName: 中),是按如下方式使用它:

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

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

这通过将 NibName 加载到内存中,然后窃取第一个元素(如果里面只有一个 UIView,那么它会选择那个元素) ,作为当从 nib 文件加载 UITableViewCells 时,这是自动释放的,这更有意义,因为您显然只关心视图本身,而不是控制器。

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.

[旋木] 2024-12-08 22:57:00

删除它后,添加一个调用

[self autorelease];

After you remove it, add a call to

[self autorelease];
蓝梦月影 2024-12-08 22:57:00

视图不知道它们的视图控制器,除非作为对委托的弱引用。这是为了避免循环引用等原因。 VC 通常在其视图之外有自己的生活 - 因此,有 viewDidLoad 和 viewDidUnload 消息。例如,在选项卡栏应用程序的整个生命周期中,每个选项卡的 VC 可能会经历许多不同的视图实例,而永远不会被释放。因此,您应该避免让视图释放自己的视图控制器。

通常,分配 VC 的类应该是释放它的类。在您提供的代码中,您有:

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

上述代码所在的控制器类可能是最适合发布 VC 的地方。您可能需要为此目的设计一个委托调用。

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.

弥枳 2024-12-08 22:57:00

[self.viewremoveFromSuperView];应该从记忆中释放你所说的观点。但请注意,如果您的视图已被任何其他对象保留,且其保留计数大于 1,则情况并非如此。另请参阅此线程上的第二个答案。

UIView的removeFromSuperView方法是否从内存中删除UIView

[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文