AllinMobile/AIMObservers

语言: Objective-C

git: https://github.com/AllinMobile/AIMObservers

AIM团队使用的通知和KVO观察员
Notifications and KVO observers used by the AIM team
README.md (中文)

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

根据'AIMObservers'

然后打电话

pod安装

并导入

#import“AIMNotificationObserver.h”

要么

#import“AIMObserver.h”

在示例中,AIMNotificationObserver用于更改主视图的背景,作为响应通知,AIMObserver用于更改按钮文本颜色,当背景发生变化时(非常蹩脚的使用,但在现实生活中,您可以使用)通知和KVO以更复杂的方式)。

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

AIMObservers

Notifications and KVO observers used by the AIM team

AIMNotificationObserver is a notification observer that should be used as alternative for [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:name object:sender]. AIMObserver is a KVO observer that should be used as an alternative for [obj addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:NULL];. It is guaranteed that observer is removed when the object is deallocated.

Usage

NSNotificationCenter observer:

@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 observer:

@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

Installation

Use the CocoaPods.

Add to your Podfile

pod 'AIMObservers'

and then call

pod install

and import

#import "AIMNotificationObserver.h"

or

#import "AIMObserver.h"

Example

In the example, AIMNotificationObserver is used to change the background of the main view, as respond to a notification and AIMObserver is used to change button text color, when background has changed (pretty lame usage, but in a real life application, you could use notification and KVO in a much more complicated way).