本文只作自用笔记,不喜勿喷,诚谢纠错。

1.发出通知 — 此方法写在需要发送出通知的控制器中

NotificationCenter.default.post(name: notificationname, object: nil)

2.接收通知并调用收到通知后需要调用的方法 —— 此方法和3方法写在需要接收的控制器中

 

//添加接受者

NotificationCenter.default.addObserver(self, selector: #selector(self.notif), name: notificationname, object: nil)

 

//收到通知后需要触发的事件

func notif(){

    print("收到通知")

}

 

3.当对象将要释放的时候,取消接收者

 

//系统将要释放对象前会调用的方法

deinit {

   //移除接收者

    NotificationCenter.default.removeObserver(self)

}