Where and how can I register an object to receive a Notification?

For example, when memory becomes low, the system sends a notification UIApplicationDidReceiveMemoryWarningNotification. What all Apple says in its docs at this moment. But where does this notification come from, and to which method is it sent? Or where and how do I register when I receive a notification?

+5
source share
4 answers

He goes to the notification center, where all notifications are centralized. The object that wants to receive information about this notification is registered in the notification center, informing which of the notifications he wants to receive information, and which method should be called upon receipt of the notification.

Cocoa NSNotification.

+5

, , :

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

, handleMemoryWarning :

- (void) handleMemoryWarning:(NSNotification *)notification
{
}
+17

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

, , . , .

+5

Be warned that your selector will have to accept the notification as an argument.

If you use something like @selector (handleMemoryWarning) and - (void) handleMemoryWarning {}, then the object will NOT send a notification, and you will still hold all your memory.

I was just bitten by it.

0
source

All Articles