How does iOS decide which objects are sent, didReceiveMemoryWarning message is sent?

I am working on an iPhone application in which several UIViews are dynamically added and removed from the main UIWindow.

When simulating low memory errors in the simulator, I found that not all view controllers receive a didReceiveMemoryWarning notification. Unfortunately, these are the controllers that will benefit most from the implementation of this method.

I cannot find good information on where and how the method is called. I read that it goes to "all UIViewControllers", but this is obviously not the case. Adding a breakpoint to one of the classes that receive the notification is also not particularly enlightening.

This is a complex project, but one way to add these views:

- (void) showMyView
{
  if(!myViewController){
    myViewController = [[MyViewController alloc]init];
    [window addSubview:myViewController.view];
  }
}

MyViewController - , MySuperViewController, UIViewController. ; .

, .

+3
4

.view , , . UIWindow , .

, UIWindow, UIWindow. , .

, . , , .

, , , .

+3

- , , - :

[[NSNotificationCenter defaultCenter] removeObserver:self];

- -dealloc.

, ( - , , ).

+2

[didReceiveMemoryWarning] ., . , nib loadView.

, " " "/". , , , notification, , didReceiveMemoryWarning.

, ( ). . , "" .

, , , , didReceiveMemoryWarning. , NSNotificationCenter, , UIApplicationDidReceiveMemoryWarningNotification. , :

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(didReceiveMemoryWarning)
                                             name:UIApplicationDidReceiveMemoryWarningNotification  
                                           object:nil];

UIViewController.

+1

I introduced this question, looking for a suitable observer who deals with memory warnings. For those using swift, you can register as follows:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveMemoryWarning:", name:UIApplicationDidReceiveMemoryWarningNotification, object: nil)

With callback method:

func didReceiveMemoryWarning(notification: NSNotification){
    //Action take on Notification
}

Also, make sure your own class inherits from NSObject, or you get this error:

… does not implement methodSignatureForSelector: — trouble ahead
+1
source

All Articles