简单管理 NotificationObserver,灵感来源于 cockcomb。您不会忘记移除通知。
@implementation SampleViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)receiveDidEnterBackgroundNotification:(NSNotification *)notification
{
// do something
}
- (void)dealloc
{
// You have to remove observer, but sometimes forget about it.
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
@implementation SampleViewController {
CSNNotificationObserver *_observer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_observer = [[CSNNotificationObserver alloc] initWithObserver:self selector:@selector(receiveDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)receiveDidEnterBackgroundNotification:(NSNotification *)notification
{
// do something
}
- (void)dealloc
{
// You don't have to worry about to remove observer
}
@end
@implementation SampleViewController {
id _didEnterBackgroundNotificationObserver;
id _willEnterForegroundNotificationObserver;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_didEnterBackgroundNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
// do something
}];
_willEnterForegroundNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
// do something
}];
}
- (void)dealloc
{
// You have to remove observer, but sometimes forget about it.
[[NSNotificationCenter defaultCenter] removeObserver:_didEnterBackgroundNotificationObserver];
[[NSNotificationCenter defaultCenter] removeObserver:_willEnterForegroundNotificationObserver];
}
@end
@implementation SampleViewController {
CSNNotificationObserver *_observer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_observer = [[CSNNotificationObserver alloc] initWithName:UIApplicationDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
// do something
}];
[_observer addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
// do something
}];
}
- (void)dealloc
{
// You don't have to worry about to remove observer
}
@end
当您选择基于 blocks 的通知观察时特别有用。您不需要拥有 ivars,只需要 1 个 ivars,当释放时会自动移除观察者。
使用 CocoaPods,
pod 'CSNNotificationObserver', :git => 'https://github.com/griffin-stewie/CSNNotificationObserver.git'
MIT 许可证 (MIT)
版权所有 (c) 2014 griffin-stewie
在此特此授予任何获得此软件及其相关文档副本(“软件”)的人士免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得软件的人员为此目的使用软件,受以下条件的约束:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者都不对因使用或其它交易软件或软件的使用或其它交易而产生的任何索赔、损害或其他责任承担责任,无论这种责任是基于合同、侵权或其他原因。