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
How to Convert Delegate to Observable Rxswift
iOS Tabbar Item Title Issue in iOS13
Need to Check That Braces in Given Array Are Balanced or Not
How to Restrict the Type That a Function Throws in Swift
Create Skscene Subclasses Programmatically, Without Size Info
Creating a Custom Scngeometry Polygon Plane with Scngeometryprimitivetype Polygon Crash/Error
Leading Zeros for Float in Swift
Changing Value in Nested Dictionary in Swift
What Are the Advantages/Use Cases of Optional Patterns Introduced in Swift 2
Custom Bullets from Images on Uitextview or Uilabel Swift
Is It Possible in Swift to Add Variables to an Object at Runtime
Reversing the Order of a String Value
How to Detect That Parameter Is a Tuple of Two Arbitrary Types
Swift - 'Bool' Is Not a Subtype of 'Void'
How to Create Text File for Writing
Saving Array to Realm in Swift
How to Read and Write Data to a Text File in Swift Using Playground