Reputation: 125
I am trying to receive data from notification addObserve. But observer does not receive any data. Posting data from collectionView DidSelect and receiving from another UIViewController. it does not called.
Here is my code:
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let imageDataDict:[String: Any] = ["image": self.setTabData[id]]
print("image dict index::::", imageDataDict)
NotificationCenter.default.post(name: Notification.Name("dynamicIndex"), object: nil, userInfo: imageDataDict)
}
Receiver end UIViewController I have added this code
Add this in ViewDidload
NotificationCenter.default.addObserver(self, selector: #selector(self.dynamicViewIndex(notification:)), name: Notification.Name("dynamicIndex"), object: nil)
Notification center observer objc function
@objc func dynamicViewIndex(notification: NSNotification) {
// handling code
print("dynamic index value observered", notification)
if let dict = notification.userInfo as NSDictionary? {
if let ValueID = dict["image"] as? UInt {
print("UInt index val:::", ValueID)
}
}
}
}
Any help much appreciated please...
Upvotes: 0
Views: 144
Reputation: 100549
For this method
@objc func dynamicViewIndex(notification: NSNotification) {
to be called the vc that contains it must be shown/active when the collection's didSet is triggered and if it isn't shown/active then the correct pattern is to use segue/delegate
Upvotes: 1