name:通常状况下咱们须要定义成一个常量, 如:kNotiAddPhotocss
object:(谁发送的通知) 通常状况下咱们能够不传,置为nil表示<匿名发送> ,若是咱们只须要传入一个参数的话,好比说自己控制器或者该类中的某一个控件的话,咱们就可使用object传出去,例子以下swift
NotificationCenter.default.post(name: <#T##NSNotification.Name#>, object: <#T##Any?#>)
// 因为事件要多级传递,因此才用通知,代理和回调其实也是能够的 NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotiAddPhoto), object: nil)
只传入一个object:参数的控件:less
@IBAction func closeBtnClick() { NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotiRemovePhoto), object: imageView.image) }
传入多个参数须要使用userInfo传入:post
NotificationCenter.default.post(name: <#T##NSNotification.Name#>, object: <#T##Any?#>, userInfo: <#T##[AnyHashable : Any]?#>)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "你定义的名字name"), object: nil, userInfo:["name" : "XJDomain", "age" : 18])
NotificationCenter.default.addObserver(<#T##observer: Any##Any#>, selector: <#T##Selector#>, name: <#T##NSNotification.Name?#>, object: <#T##Any?#>)
// 接受添加图片的通知 NotificationCenter.default.addObserver(self, selector: #selector(addPhoto), name: Notification.Name.init(rawValue: kNotiAddPhoto), object: nil)
上面通知调用方法: 没有参数的方法ui
// 添加照片 @objc fileprivate func addPhoto(){ // 方法调用 }
--------------------------------------------------------------spa
// 接受删除图片的通知 NotificationCenter.default.addObserver(self, selector: #selector(removePhoto(noti:)), name: NSNotification.Name(rawValue: kNotiRemovePhoto), object: nil)
上面通知调用方法: 有参数的方法线程
// 删除照片 @objc fileprivate func removePhoto(noti : Notification) { guard let image = noti.object as? UIImage else { return } guard let index = images.index(of: image) else { return } images.remove(at: index) picPickerCollectionView.images = images }
在接收通知控制器中移除的,不是在发送通知的类中移除通知的代理
// 移除通知<通知移除是在发通知控制器中移除> deinit { NotificationCenter.default.removeObserver(self) }
在接收通知的时候有另一个方法:code
好处是:不须要咱们再写一个方法server
坏处是:移除通知相比麻烦,可是还好
// name: 通知名字 // object :谁发出的通知,通常传nil // queue :队列:表示决定block在哪一个线程中去执行,nil表示在发布通知的线程中执行 // using :只要监听到了,就会执行这个block // 必定要移除,不移除的话会存在坏内存访问,移除的话须要定义一个属性observe来接收返回对象,而后在移除方法中移除便可 observe = NotificationCenter.default.addObserver(forName: Notification.Name.init(rawValue: kNotiAddPhoto), object: nil, queue: nil) { (Notification) in print("-------------",Thread.current) }
移除通知:
01-先定义一个属性
weak var observe : NSObjectProtocol?
02-接收通知返回来的观察者
observe = NotificationCenter.default.addObserver(forName: Notification.Name.init(rawValue: kNotiAddPhoto), object: nil, queue: nil) { (Notification) in print("-------------",Thread.current) }
03-在移除通知方法中移除通知
// 移除通知<通知移除是在发通知控制器中移除> deinit { //NotificationCenter.default.removeObserver(self) NotificationCenter.default.removeObserver(observe!) }