The root level view controller in my iPad application is a UISplitViewController. Thus, it has 2 view controllers :
- one master view controller (item 0 of the viewControllers property)
- one detail view controller (item 1 of the viewControllers property)
The detail view controller is a custom view controller that I change depending on taps and events in my master view controller.
To change this detail view controller, I use the following code:
- (void)replaceSecondViewControllerBy:(UIViewController *)viewController {
[[self.viewControllers objectAtIndex:1] dismissModalViewControllerAnimated:NO];
NSArray *newVC = [NSArray arrayWithObjects:[self.viewControllers objectAtIndex:0], viewController, nil];
self.viewControllers = newVC;
}
My problem is that when my app receives a memory warning event, the didReceiveMemoryWarning method is called for all my view controllers, except for former detail view controllers. And they’re not being deallocated because they still are delegates for other objects (including asynchronous methods that might still be running).
My questions are :
- What are the rules for a UIViewController to receive a
didReceiveMemoryWarningmessage ? Why don’t my former detail view controllers receive them ? - Can I safely call
didReceiveMemoryWarningorviewDidUnloadmyself on these old view controllers ?
It looks like a
UIViewControllersubscribes toUIApplicationDidReceiveMemoryWarningNotificationwhen it is created. It removes observing the notification when it is deallocated. SodidReceiveMemoryWarningis called even if the the controller’s view is not in the view hierarchy. So make sure your controllers are not deallocated.It is unlikely but if you happen to be using the following code to remove notification observation from your controllers they will also stop listening memory notifications.