AIMNotificationObserver 0.3

AIMNotificationObserver 0.3

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年12月

Maciej Gad维护。



AIMObservers

AIM团队使用的通知和KVO观察者

AIMNotificationObserver是一个通知观察者,应作为替代[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:name object:sender]使用。AIMObserver是一个KVO观察者,应作为替代[obj addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:NULL];使用。保证对象被释放时观察者将被移除。

使用

NSNotificationCenter观察者

@interface ViewController ()
@property (strong, nonatomic) AIMNotificationObserver *observer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak __typeof(self) weakSelf = self;
    self.observer = [AIMNotificationObserver observeName:@"changeBackground" onChange:^(NSNotification *notification) {
        //use weakSelf to avoid strong reference cycles
        weakSelf.view.backgroundColor = [UIColor red];
    }];
}

@end

KVO观察者

@interface ViewController ()
@property (strong, nonatomic) AIMObserver *observer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak __typeof(self) weakSelf = self;
    self.observer = [AIMObserver observed:self.view.layer keyPath:@"backgroundColor" onChange:^(NSDictionary *change) {
        //use weakSelf to avoid strong reference cycles
        [weakSelf.button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }];
}

@end

安装

使用CocoaPods

添加到Podfile

pod 'AIMObservers'

然后调用

pod install

导入

#import "AIMNotificationObserver.h"

#import "AIMObserver.h"

示例

在示例中,AIMNotificationObserver用于更改主视图的背景,作为响应一个通知,而AIMObserver用于在背景更改时更改按钮文字颜色(使用非常简单,但在真实的应用程序中,您可以使用更复杂的样式使用通知和KVO)。