7

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!

tshepang
  • 12,111
  • 21
  • 91
  • 136
Adithya
  • 73
  • 1
  • 1
  • 3
  • 2
    This question should be removed. viewDidUnload is deprecated and no longer called in iOS 6. – Rose Perrone Jan 02 '13 at 23:29
  • 1
    Yes, but not everybody is solely developing for iOS 6, so we should at most note that this is deprecated and therefore not applicable to iOS 6, yet continue to consider it a valid question and answer for those developing for other versions of iOS. – ekinnear May 10 '13 at 03:04

4 Answers4

11

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.

cduhn
  • 17,818
  • 4
  • 49
  • 65
3

viewDidUnload is called when a UIViewControllers's view is unloaded, not when a subview is removed from view.

freespace
  • 16,529
  • 4
  • 36
  • 58
0

viewdidunload is a method of a UIViewController. If you are using a UIViewController subclass to create an object and that obect's view is removed from superview then viewdidunload will be called. Make sure that your child view is made from a UIViewController.

Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
  • My child is inherited from UIViewController only. But it is not getting called.Anymore suggestions? – Adithya Apr 12 '11 at 05:13
0

An additional note is that viewDidUnload may be not executed before dealloc. This happens when the UIViewController reference count reach 0.

As the Xcode template says:

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
Freeman
  • 5,810
  • 3
  • 47
  • 48