Latest 0.0.1
Homepage https://github.com/laichanwai/YFMessageCenter
License MIT
Platforms ios 8.0
Authors

基于 Protocol 实现通知机制,去掉硬编码,相比传统 NSNotificationCenter 更加的安全、优雅。

使用说明

old

// 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];

new

// 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];

Latest podspec

{
    "name": "YFMessageCenter",
    "version": "0.0.1",
    "summary": "iOS u57fau4e8e Protocol u7684u6d88u606fu6d3eu53d1u7ec4u4ef6",
    "description": "YFMessageCenter is a part of YFKit",
    "homepage": "https://github.com/laichanwai/YFMessageCenter",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "laizw": "[email protected]"
    },
    "source": {
        "git": "https://github.com/laichanwai/YFMessageCenter.git",
        "tag": "0.0.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "YFMessageCenter/**/*.{h,m}"
}

Pin It on Pinterest

Share This