Post

Replies

Boosts

Views

Activity

How to readd a notification observer
So I have about four notification observers added in a particular view controller, but I want to remove one in particular if a condition is met and then add it back for a different condition. I was able to remove the particular observer but I have had trouble adding it back. the other ones work except the one a removed and added back and I know it's working cos when the VC loads and all the notification observers are added but then it stops working after adding it back. here's my code: registered the observer in ViewDIdAppear and this registers my Bluetooth device and whenever I tap on the Bluetooth device button this function gets called NotificationCenter.default.addObserver( self, selector: #selector(Myclass.deviceTapped), name: Notification.Name("bluetoothRecievedNoticicationName"), object: nil) I removed the observer here if removeOberver == true { NotificationCenter.default.removeObserver(self, name: Notification.Name("bluetoothRecievedNoticicationName"), object: nil) } and then use this to add it back again if removeOberver == false { NotificationCenter.default.addObserver( self, selector: #selector(Myclass.deviceTapped), name: Notification.Name("bluetoothRecievedNoticicationName"), object: nil) } and after adding this ^ back, the deviceTapped function doesn't get called anymore
1
0
983
Mar ’21