這里主要是對比一下2中監(jiān)聽方式
第一種:
發(fā)送通知(參數(shù)的組合實質(zhì)就是一個NSNotification)
NSNotificationCenter.defaultCenter().postNotificationName("oneNotification", object: "liyang", userInfo: ["li":"yang"])
監(jiān)聽通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentDegreeShowVC.noticeAcion(_:)), name: "oneNotification", object: nil) //object必須為空(否則監(jiān)聽不了),暫時不清楚這個參數(shù)的意義话原。
移除通知
NSNotificationCenter.defaultCenter().removeObserver(self)
第二種:
發(fā)送通知(參數(shù)的組合實質(zhì)就是一個NSNotification)
NSNotificationCenter.defaultCenter().postNotificationName("oneNotification", object: "liyang", userInfo: ["li":"yang"])
監(jiān)聽通知
var observe = NSNotificationCenter.defaultCenter().addObserverForName("oneNotification", object: nil, queue: NSOperationQueue.mainQueue()) { (n:NSNotification) in
print("\(self)我收到通知了:object:\(n.object),userInfo:\(n.userInfo)")
}
移除通知
NSNotificationCenter.defaultCenter().removeObserver(self.observe)
注意:self.observe == observe
mark:上面的移除方式注意配對使用,否則無效
EventBox是基于第二種的使用