YFMessageCenter 0.0.1

YFMessageCenter 0.0.1

laizw 维护。



  • 作者:
  • laizw

YFMessageCenter

基于 Protocol 实现 notification 机制,移除硬编码,比传统的 NSNotificationCenter 更为安全、优雅。

使用说明

// 1. 注册
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:kChangTextNotification object:nil];

// 2. 实现
- (void)changeText:(NSNotification *)noti {
    self.titleLabel.text = noti.object;
}

// 3. 发送
[[NSNotificationCenter defaultCenter] postNotificationName:kChangTextNotification object:@"hello"];

// 4. 移除
[[NSNotificationCenter defaultCenter] removeObserver:self name:kChangTextNotification object:nil];

// 1. 定义
@protocol ChangeTextProtocol <NSObject>
- (void)changeText:(NSString *)text;
@end

// 2. 监听
OBSERVE_MESSAGE(self, ChangeTextProtocol);

// 3. 实现协议
- (void)changeText:(NSString *)text {
    self.titleLabel.text = text;
}

// 4. 分发消息
[DISPATCH_MESSAGE(ChangeTextProtocol) changeText:self.textField.text];

// 5. 移除
UN_OBSERVE_MESSAGE(self, ChangeTextProtocol);

协议继承

@protocol ChangeTextProtocol2 <ChangeTextProtocol>
- (void)changeText2:(NSString *)text;
@end

// 使用当前协议定义的方法 ✅
[DISPATCH_MESSAGE(ChangeTextProtocol2) changeText2:self.textField.text];

// 使用继承协议中的方法 ✅
[DISPATCH_MESSAGE(ChangeTextProtocol2) changeText:self.textField.text];