1, 声明一个全局的通知,在 class 外面
let NotifyChatMsgRecv = NSNotification.Name(rawValue:"notifyChatMsgRecv");
2, 开始 接收类 中 添加 监听和 接收 处理 方法
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
//添加监听
NotificationCenter.default.addObserver(self, selector:#selector(didMsgRecv(notification:)),name: NotifyChatMsgRecv, object: nil);
}
//接收到通知的处理
func didMsgRecv(notification:NSNotification){
print("didMsgRecv: \(notification.object)");
}
//
}
3, 发送通知
NotificationCenter.default.post(name:NotifyChatMsgRecv, object: "123", userInfo: nil)