viewdidunload根本没有被调用 [英] viewdidunload is not getting called at all

查看:91
本文介绍了viewdidunload根本没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是这样的:


  1. 父类加载子视图。

  2. Child在操作期间使用removeFromSuperView删除自身。

  3. 当子项被删除时,父类会获得委托调用,从而使子对象为零。

仍未调用子类的viewdidunload!

解决方案

与其他人一样已经提到过,viewDidUnload是UIViewController的一个方法。不能保证被叫。当应用程序释放控制器的视图以释放内存时,它仅在低内存情况下被调用。这使您有机会发布您可能保留为控制器属性的任何视图的子视图。



如果您在视图控制器的视图上调用removeFromSuperview,然后你可能在它没有设计处理的上下文中使用UIViewController。视图控制器旨在管理全屏视图。预计这些视图将在iPhone上的少数几种情况下呈现:


  1. 作为窗口根视图的全屏视图controller

  2. 作为由UINavigationController管理的屏幕层次结构中的全屏视图

  3. 作为选项卡中显示的全屏视图

  4. 使用presentModalViewController呈现全屏视图:动画:

只要您使用视图控制器在这些上下文中(加上iPad上的其他几个上下文),您可以通过loadView,viewDidLoad,viewWillAppear:animated:,viewDidAppear:animated:,viewWillDisappear:animated:和viewDidDisappear:animated:,可靠地管理视图的生命周期。和viewDidUnload方法(再次,请记住,不会总是调用viewDidUnload)。



你应该通常将视图控制器的视图传递给addSubview:是时候您添加根视图控制器的视图到窗口。如果您想尝试使用嵌套视图控制器来管理非全屏子视图,则必须在适当的时间从全屏视图的控制器中手动调用其viewWill / DidAppear / Disappear:animated:和viewDidUnload方法。 / p>

My problem is like this:

  1. Parent class loads a child view.
  2. Child removes itself during an action by using "removeFromSuperView".
  3. Parent class gets a delegate call when the child gets removed and hence makes the child object nil.

Still the child class's viewdidunload is not called!

解决方案

As others have mentioned, viewDidUnload is a method of UIViewController. It isn't guaranteed to get called. It only gets called in low memory situations when the app releases the controller's view to free up memory. This gives you an opportunity to release any of the view's subviews that you may have retained as properties of your controller.

If you're calling removeFromSuperview on your view controller's view, then you're probably using UIViewController in a context it wasn't designed to handle. View controllers are designed to manage full-screen views. These views are expected to be presented in just a handful of contexts on the iPhone:

  1. As the full-screen view of the window's root view controller
  2. As a full screen view in a hierarchy of screens managed by UINavigationController
  3. As a full screen view presented within a tab
  4. As a full screen view presented using presentModalViewController:animated:

As long as you're using the view controller in these contexts (plus a couple other contexts on iPad), then you can reliably manage your view's life-cycle via the loadView, viewDidLoad, viewWillAppear:animated:, viewDidAppear:animated:, viewWillDisappear:animated:, and viewDidDisappear:animated:, and viewDidUnload methods (again, bearing in mind that viewDidUnload won't always get called).

The only time you should typically pass a view controller's view to addSubview: is when you add your root view controller's view to the window. If you want to try to use a nested view controller to manage a non-fullscreen subview, you'll have to call its viewWill/DidAppear/Disappear:animated: and viewDidUnload methods manually at appropriate times from your full-screen view's controller.

这篇关于viewdidunload根本没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆