Created
January 3, 2012 21:55
-
-
Save quantumpotato/1557144 to your computer and use it in GitHub Desktop.
NSNotification observing with blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSNotificationCenter (CHAdditions) | |
+ (void)addObserver:(id)observer forNotificationNamed:(NSString *)notificationName withBlock:(void (^)(NSNotification *note))block; | |
@end | |
#import "NSNotificationCenter+CHAdditions.h" | |
@implementation NSNotificationCenter (CHAdditions) | |
+ (void)addObserver:(id)observer forNotificationNamed:(NSString *)notificationName withBlock:(void (^)(NSNotification *note))block { | |
[[NSNotificationCenter defaultCenter] addObserverForName:notificationName | |
object:nil | |
queue:[NSOperationQueue mainQueue] | |
usingBlock:block]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Considered a category on NSObject but felt that violated the purpose of NSNotificationCenter.
Slightly less messy, doesn't extend your notification handling code so far to the right in your editor.