现在的位置: 首页 > 综合 > 正文

viewDidUnload not gauranteed

2013年04月26日 ⁄ 综合 ⁄ 共 1443字 ⁄ 字号 评论关闭

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.

抱歉!评论已关闭.