AddingRemoving an Observer with a Block

suggest change

Instead of adding an observer with a selector, a block can be used:

id testObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"TestNotification"
                                                                    object:nil 
                                                                     queue:nil 
                                                                usingBlock:^(NSNotification* notification) {
    NSDictionary *userInfo = notification.userInfo;
    MyObject *myObject = [userInfo objectForKey:@"someKey"];
}];

The observer can then be removed with:

[[NSNotificationCenter defaultCenter] removeObserver:testObserver 
                                                name:@"TestNotification"
                                              object:nil];

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents