gpt4 book ai didi

ios - NotificationCenter 没有调用选择器

转载 作者:行者123 更新时间:2023-11-28 18:48:34 27 4
gpt4 key购买 nike

我已经搜索过了,但问题仍然存在。我找到了 this great question但不幸的是它对我不起作用。这是我第一次使用 NotificationCenter,当我想将数据传递给 XLPagerTabStrip 选项卡下的 View Controller 时,需要首先使用它。 .

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

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

选择器方法是:

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

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

最佳答案

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

关于ios - NotificationCenter 没有调用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46440481/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com