Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where and how do I register an object for receiving a Notification?

For example, when memory gets low, the System sends a UIApplicationDidReceiveMemoryWarningNotification notification. That's all Apple says in its docs at that point. But where does this notification come from, and to which method is it sent? Or where and how do I register what that I get notified?

like image 284
Thanks Avatar asked Apr 11 '09 18:04

Thanks


2 Answers

From within the initialization code of the class you wish to receive the notification make the following method call:

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

This assumes that your class also implements a handleMemoryWarning method as follows:

- (void) handleMemoryWarning:(NSNotification *)notification
{
}
like image 76
m4rkk Avatar answered Oct 04 '22 17:10

m4rkk


Much simpler to use the application delegate and implement the optional method

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

Most common notifications are also available translated into calls to a delegate, typically to optional methods in a formal protocol. Your delegate can be whatever object you like.

like image 26
Jim Avatar answered Oct 04 '22 17:10

Jim