iOS NSNotificationCenter Adding/Removing an Observer with a Block


Example

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];