在 didReceiveMemoryWarning 和 viewDidUnload 之后使用 viewDidLoad

发布于 2025-01-01 06:46:12 字数 1430 浏览 1 评论 0原文

我仍在尝试了解发生内存警告时管理视图的过程。我在这个答案中收到了一些很好的信息,但我还有疑问。

假设我有一个视图控制器 VC1,其中包含由视图控制器 VC2 管理的子视图(该子视图具有其视图属性)。最初,如果我想将 VC2 视图放入另一个 VC1 视图中,那么我可能会这样做:

UIView *VC2 = [UIView alloc] initWithFram...];
VC2.delegate = self;
... // other references to VC2, but not to it's view, yet.
[VC1.view addSubview VC2.view];  // At this point VC2 loadView is called automatically,
                                 // followed by VC2 viewDidLoad.

在一段时间后,VC2 中收到内存警告。因此调用了VC2的didReceiveMemoryWarning,然后调用了VC2的viewDidUnload。

我的理解到此就结束了(如果我已经说的是正确的话!)

我期望神奇地发生的是VC2中的视图及其资源可以被释放(例如,可能是选项卡栏控制器引用的一个视图) ,但目前未显示),如果可以通过 VC2 的 viewDidLoad 方法重新创建它,那么应该可以正常工作。假设VC2的视图暂时不可见,应该是release的。

它到底是如何被释放的? didReceiveMemoryWarning 在 VC2 中执行。 VC2有没有发布自己的观点?如果没有,应该会发生什么。

第二个问题,如果VC2的视图已经被释放,假设再次需要该视图(就像有人在选项卡栏上选择了相应的选项卡)。我的理解是,如果引用 VC2 的 view property ,则会调用 VC2 loadView 。最初,当 VC1 使用属性引用将其添加为子视图时,它被引用。选项卡栏控制器可以通过视图控制器 VC2 通过引用来调用它,该控制器位于选项卡栏控制器的 viewControllers 数组中。所以我猜标签栏控制器将引用 view 属性,这会导致 VC2 loadView 被调用。

好吧,我回答了我的第二个问题,并且可能同时回答了它。有人可以确认我是否理解正确吗?

另外,我仍然不清楚(如我的第一个问题)VC2 视图应该如何以及在哪里发布。

是否有人可以向我指出一个教程,该教程会逐步完成释放视图和视图层次结构以响应内存警告的整个过程,并解释如何在需要时重新构建已释放的视图?这确实有助于我理解这个过程。

I am still trying to understand the process of managing views when meory warnings occur. I received some good information in this answer, but I still have questions.

Suppose I have a view controller VC1 that contains a subview that is managed by view controller VC2 (which has that subview it's view property). Initially, if I want to put the VC2 view into another the VC1 view, so I might do this:

UIView *VC2 = [UIView alloc] initWithFram...];
VC2.delegate = self;
... // other references to VC2, but not to it's view, yet.
[VC1.view addSubview VC2.view];  // At this point VC2 loadView is called automatically,
                                 // followed by VC2 viewDidLoad.

At some time later, a memory warning is received in VC2. So VC2's didReceiveMemoryWarning is called, followed by VC2's viewDidUnload.

This is where my understanding ends (if what I have already said is correct!)

What I expect to magically happen is that the view in VC2 and its resources can be released (for example, it may be one view that the tab bar controller references, but is currently not being shown), and this should work out alright if it can all be recreated by VC2's viewDidLoad method. Assuming that VC2's view is not vissible for the time being, it should be release.

How, exactly does it get released? The didReceiveMemoryWarning executes in VC2. Does VC2 release its own view? If not, what is suppose to happen.

Second question, if VC2's view has been released, suppose the view is needed again (like someone selectes the corresponding tab on the tab bar). My understanding is that VC2 loadView gets called if view property of VC2 is referenced. Initially it was referenced when VC1 added it as a subview using the property reference. A tab bar controller may call it up by reference through the view controller VC2, which is in the tab bar controller's viewControllers array. So I guess the tab bar controller will reference the view property, and that leads to VC2 loadView being called.

Well I walked through my second question, and may have answered it at the same time. Can someone confirm whether I understand this correctly?

Also, I am still not clear (as in my first question) how and where VC2 view should be released.

Is there a tutorial someone can point me to that walks through this whole process of releasiing views and view hierarchies in response to memory warnings, also expalining how the released views are reconstituted when they are needed? That would really help my understanding of this process.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文