NotificationCenter未调用选择器

时间:2017-09-27 06:00:00

标签: ios swift3 observers notificationcenter

我已经对此进行了搜索,但问题仍然存在。我发现this great question但不幸的是它对我没用。这是我第一次使用NotificationCenter,并且当我想在XLPagerTabStrip的标签下将数据传递给viewcontroller时,首先需要使用它。

以下是我发布通知的方式:

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

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

在我观察此通知的课程中,我打电话给NotificationCenter.default.addObserver(self, selector: #selector(gotDocID), name: Notification.Name("docID"), object: nil)

选择器方法是:

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)
  }
}

我还尝试添加观察者: NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notification:)), name: Notification.Name("docID"), object: nil)但结果仍然相同。

问题在于func gotDocID(notification:NSNotification)未被调用。

更新

发布通知的类是ViewController.swift,拥有观察者的类是AvailableViewController.swift

根据评论我已将观察者更改为NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notific‌​ation:)), name: Notification.Name("NotificationIdentifier"), object: nil),并生成此错误。 enter image description here 并且通过执行以下操作我得到了同样的错误。 enter image description here

  

类型的值&#39; AvailableViewController&#39;没有会员&got'ocid&#39;

4 个答案:

答案 0 :(得分:1)

您可以使用以下代码发布和获取数据。

//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   
  }
 }

答案 1 :(得分:1)

@objc添加到您的功能

@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);

答案 2 :(得分:1)

为什么不尝试关闭

确保您的帖子通知发生。

更改

 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>)

        }

答案 3 :(得分:1)

请检查:

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)
        }
    }
}