Where to remove observer for NSNotification in Swift?

后端 未结 9 1479
你的背包
你的背包 2020-12-01 00:37

Where should I remove the observer for NSNotification in Swift, since viewDidUnload and dealloc() are unavailable?

9条回答
  •  旧时难觅i
    2020-12-01 01:22

    In Swift 4.2, this is one of the way you can remove observer

    deinit {
        NotificationCenter.default.removeObserver(self, name: Notification.Name.Identifier, object: nil)
    }
    

    setup addObserver notification in viewDidLoad class

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(didReceivedItemDetail), name: Notification.Name.Identifier, object: nil)
    }
    

提交回复
热议问题