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

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

问题描述

我的问题是这样的:

  1. 父类加载子视图.
  2. 子在操作期间使用removeFromSuperView"移除自身.
  3. 当子对象被移除时,父类会得到一个委托调用,从而使子对象为零.

仍然没有调用子类的viewdidunload!

Still the child class's viewdidunload is not called!

推荐答案

正如其他人提到的,viewDidUnload 是 UIViewController 的一个方法.不能保证被调用.只有在应用程序释放控制器的视图以释放内存时才会在内存不足的情况下调用它.这使您有机会释放您可能保留为控制器属性的任何视图子视图.

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.

如果您在视图控制器的视图上调用 removeFromSuperview,那么您可能是在其设计无法处理的上下文中使用 UIViewController.视图控制器旨在管理全屏视图.这些视图预计将在 iPhone 上的少数上下文中呈现:

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. 作为窗口根视图控制器的全屏视图
  2. 作为 UINavigationController 管理的屏幕层次结构中的全屏视图
  3. 作为选项卡中显示的全屏视图
  4. 作为使用 presentModalViewController:animated 呈现的全屏视图:

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

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).

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

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.

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

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