Swift 4 - Notification Center Addobserver Issue

Swift 4 - Notification Center addObserver issue

You can improve your code with these steps:

extension Notification.Name {
static let dataDownloadCompleted = Notification.Name(
rawValue: "dataDownloadCompleted")
}

And use it like this:

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self,
selector: #selector(YourClass.sayHello),
name: .dataDownloadCompleted,
object: nil)

But as was already pointed out, issue is solved by changing to #selector

NSNotificationCenter addObserver in Swift

It's the same as the Objective-C API, but uses Swift's syntax.

Swift 4.2 & Swift 5:

NotificationCenter.default.addObserver(
self,
selector: #selector(self.batteryLevelChanged),
name: UIDevice.batteryLevelDidChangeNotification,
object: nil)

If your observer does not inherit from an Objective-C object, you must prefix your method with @objc in order to use it as a selector.

@objc private func batteryLevelChanged(notification: NSNotification){     
//do stuff using the userInfo property of the notification object
}

See NSNotificationCenter Class Reference, Interacting with Objective-C APIs

NotificationCenter - addObserver not called

If, at the time ViewController comes into existence, secondvc does not yet exist, then there is no one there to receive the posted notification and that is why you don't see the notification being received later when secondvc does come into existence.

NotificationCenter obersever not called in swift 4

Change the notification post from

 NotificationCenter.default.post(name: Notification.Name("MessageReceived"), object: message)

to

NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "MessageReceived"),object: message))

and the observer from

NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: Notification.Name("MessageReceived"), object: nil)

to

NotificationCenter.default.addObserver(self, selector: #selector(self.messagereceived(notification:)), name: NSNotification.Name(rawValue: "MessageReceived"), object: nil)

NotificationCenter.default.addObserver is not calling the objective C function

This is a very incorrect way to use Notifications.

What you want to do is pass your "sessionID" value to from VC-A to VC-B when VC-A is moving to / presenting VC-B.

Assuming you are using a segue...

Based on what you posted, you already have a variable / property in VC-B, probably:

var session_ID: [String] = ""

So, in VC-A, you would do something like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nextVC = segue.destination as? VCB {
nextVC.session_ID = self.uid
}
}

Notification Center not working. The observer is not being called

Function definition is not correct. It should be :

@objc func toggleSideMenu(_ notification: Notification){
if isMenuOpen {
sideContainer.constant = -260
} else {
sideContainer.constant = 0
}
}

Call it using :

NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu(_:)), name: NSNotification.Name("callToggleSideMenu"), object: nil)


Related Topics



Leave a reply



Submit