本文共 576 字,大约阅读时间需要 1 分钟。
*****注册通知、监听通知
NotificationCenter.default.addObserver(self, selector: #selector(noti(noti:)), name: NSNotification.Name(rawValue: "home"), object: nil)
注意:NSNotification.Name(rawValue: "home")是通知的名字home
@objc func noti(noti:Notification){
let dict:[String:String]=noti.userInfo as! [String : String]
print("首页收到通知了\(dict["home"]!)")
}
***************发送通知:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "home"), object: nil, userInfo: ["home":"homestr"])
**************销毁通知
在析构函数中销毁通知的监听
deinit{
NotificationCenter.default.removeObserver(self)//移除对通知的监听
}
转载地址:http://mimgi.baihongyu.com/