Notification
通知(Notification)常使用在异步跨前程数据传输的过程中,如控制层接收到数据后通过Notification传输到界面层。
Notification的使用方法如下:
//通知名称常量
let NotifyMsgName = NSNotification.Name(rawValue:"notifyMsg")
//注册通知监听
NotificationCenter.default.addObserver(self, selector:#selector(notifiAction(notification:)),name: NotifyMsgName, object: nil)
//通知处理函数
func notifiAction(notification:NSNotification){
let userinfo = notification.userInfo
}
//post通知
NotificationCenter.default.post(name:NotifyMsgName, object: nil, userInfo: ["name":"xiaoming"])
//注销通知,在controller退出的时候注销
deinit {
NotificationCenter.default.removeObserver(self)
}
可将使用Notification的类封装后供子类继承,实现通知多传。