objective-cuipopovercontrollerviewdidunload

UIPopoverController's contentViewController.viewDidUnload not getting called


When I use a UIPopoverController and give it a contentViewController, I cannot seem to get the contentViewController to correctly be deallocated (as evidenced by the fact that the contentViewController.viewDidUnload is never getting called).

Code to create and display the popup:

PopupTestViewController *popupTest = [[PopupTestViewController alloc] initWithNibName:@"PopupTestViewController" bundle:nil];
popupTest.mainViewController = self;
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popupTest];
self.popoverController.popoverContentSize = popupTest.view.frame.size;
self.popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Now I am assuming that the contentViewController (in the code above, this is PopoverTestViewController) should be deallocated when the UIPopoverController is closed (whether by clicking off of it, or be explicitly dismissing it). But viewDidUnload is never called. I did notice, however, that if I define a dealloc method for PopoverTestViewController, that is called appropriately.

So my question is: why is viewDidUnload never getting called? (And I'm using ARC).


Solution

  • viewDidUnload is not guaranteed to get called. It only gets called when the application receives a memory warning and the receiving ViewController has view loaded but is off screen. When the view is loaded and retain count reaches zero, viewDidUnload is not called.

    You can find more details in the documentation. Instructions on when to release what objects can be found in the same document.