1

I've searched about this but the problem still exist for me. I found this great question but unfortunately it didn't work for me. This is the first time I'm working with NotificationCenter and the need to use this first occurs when I wanted to pass data to a viewcontroller under a tab of XLPagerTabStrip.

So here is how I am posting the Notification:

if let doc_ID = mainDoctorsArray[sender.tag].doctors_id {

            NotificationCenter.default.post(name: Notification.Name("docID"), object: nil, userInfo: ["value" : doc_ID])
        }

In the class I've made for observing this notification I'm calling NotificationCenter.default.addObserver(self, selector: #selector(gotDocID), name: Notification.Name("docID"), object: nil)

The selector method is:

func gotDocID(notification:NSNotification) {

let userInfo:Dictionary<String,String> = notification.userInfo as! Dictionary<String,String>

if let item = userInfo["value"] {
    getDoctorDetails(docID: Int(item)!)
    //print(item,self)
  }
}

I've also tried adding observer as: NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notification:)), name: Notification.Name("docID"), object: nil) but still same result.

The issue is that func gotDocID(notification:NSNotification) is not being called.

UPDATE

Class which is posting the notification is ViewController.swift and the class which has the observer is AvailableViewController.swift

Based on a comment I've changed observer to NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notific‌​ation:)), name: Notification.Name("NotificationIdentifier"), object: nil) and this error is generated. enter image description here and also by doing the follow I'm getting the same error. enter image description here

Value of type 'AvailableViewController' has no member 'gotDocID'

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116

4 Answers4

2

Add @objc to your function

@objc func gotDocID(notification:NSNotification) {

}

// Define identifier

let notificationName = Notification.Name("docID")

// Register to receive notification

NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notification:)), name: notificationName, object: nil)

// Post notification
NotificationCenter.default.post(name: notificationName, object: nil)

// Stop listening notification
NotificationCenter.default.removeObserver(self, name: notificationName, object: nil);
Mukesh
  • 3,680
  • 1
  • 15
  • 32
  • I don't know why but I'm getting this error: Type `AvailableViewController' has no member 'gotDocID(notific‌​ation:)` – Chaudhry Talha Sep 27 '17 at 06:39
  • I have tested the code it not giving any error set cursor after `#selector(AvailableViewController.` and check for func in autocomplete – Mukesh Sep 27 '17 at 06:45
1

You can use the below code to post and get data.

//Post notification
NSNotificationCenter.defaultCenter().postNotificationName("docID", object: nil, userInfo: ["value" : doc_ID])

//Get data from observer
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AvailableViewController.gotDocID(_:)), name: "docID", object: nil)

//Method called after notification is posted.
func gotDocID(notification: NSNotification) {
  if let image = notification.userInfo?["value"] as? String {
  // do something with your data   
  }
 }
Nupur Gupta
  • 305
  • 1
  • 12
  • I'm getting `Type 'AvailableViewController' has no member 'gotDocID'` and I'm using swift 3.2 so `NSNotificationCenter` is `NotificationCenter` which I've converted as per your code but still this error. You can also see this error in the screenshot in update section of this question. – Chaudhry Talha Sep 27 '17 at 06:31
  • @ChaudhryTalha Use self.gotDocID(_:) instead of AvailableViewController.gotDocID(notific‌​ation:) and it will work. I have check this is working – Nupur Gupta Sep 27 '17 at 06:38
1

Why dont you try closure

Make sure your post notification occures.

Change

 NotificationCenter.default.post(name: Notification.Name("docID") , object: ["value" : doc_ID])


NotificationCenter.default.addObserver(forName: Notification.Name("docID"), object: nil, queue: OperationQueue.main) { (notify) in
                        print(notify.object as!  Dictionary<String,String>)

        }
karthikeyan
  • 3,821
  • 3
  • 22
  • 45
1

Please check :

class ViewController: UIViewController  {
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(self.gotDocID(notification:)), name: Notification.Name("docID"), object: nil)
    }

    @IBAction func saveButton(_ sender: UIButton) {
        NotificationCenter.default.post(name: Notification.Name("docID"), object: nil, userInfo: ["value" : "123"])
    }

    @objc func gotDocID(notification:NSNotification) {
        let userInfo:[String: String] = notification.userInfo as! [String: String]
        if let item = userInfo["value"] {
            print(item,self)
        }
    }
}
Vini App
  • 7,339
  • 2
  • 26
  • 43