要运行示例项目,请克隆仓库,然后先从 Example 目录运行 pod install
。
#import "ASNotificationCenter.h"
// append Notification listener for some class
-(void) someMethod{
// ...
[[ASNotificationCenter protectedCenter] addObserver:self selector:@selector(notificationOut:) name:@"someNotification" object:nil];
// or use short declaration
[_protectedCenter addObserver:self selector:@selector(notificationSecondOut:) name:@"someSecondNotification" object:nil];
// ...
}
// Selectors
-(void) notificationOut:(ASNotification *) notification{
// do something with ASNotification model (ASNotificationName name, id object, NSDictionary *userInfo)
...
}
-(void) notificationSecondOut:(ASNotification *) notification{
// do something with ASNotification model (ASNotificationName name, id object, NSDictionary *userInfo)
...
// you can also unsubscribe from ASNotificationCenter with notification.name
[[ASNotificationCenter protectedCenter] removeObserver:self name:notification.name object:nil];
}
// Unsubscribe
-(void) dealloc{
// Unsubscribe all notification for this class
[_protectedCenter removeObserver:self];
}
-(void) someUnsubscribe{
// Unsubscribe single notification for the name in this class
[[ASNotificationCenter protectedCenter] removeObserver:self name:@"someNotification" object:nil];
}
ASNotificationCenter 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "ASNotificationCenter"
Aleksey Anisimov,[email protected]
ASNotificationCenter 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。