Search code examples
iosswiftuicollectionviewuicollectionviewcellnsnotificationcenter

iOS 13 Remove Observer in UICollectionViewCell custom Class


Hi everyone I need to add 2 observers in my UICollectionViewCell Custom Class. I implemented the two observers in this way

override init(frame: CGRect) {
     super.init(frame: frame)

     NotificationCenter.default.addObserver(self, selector: #selector(disablePreviousMinutes), name: NSNotification.Name(rawValue: "NotificationIdentifier"), object: nil)

     NotificationCenter.default.addObserver(self, selector: #selector(enableMinute), name: NSNotification.Name(rawValue: "NotificationIdentifier2"), object: nil)
}

Now I would like to know how I can remove observers from my UICollectionViewCell class

I tried calling override func willMove(toWindow newWindow: UIWindow?) But in this way all the inserted functions are repeated for all the cells

What is the best way to remove an observer from a UICollectionViewCell custom class?


Solution

  • Just giving a short write-up so you can mark this question as solved.

    As you found out, since iOS 9 you don’t need to manually remove observers from NotificationCenter. they are automatically removed.

    If you still want to remove them, you can safely do so in deinit()