通知中心并没有叫我搜索这个,但问题依然存在,我选择
。我发现this great question,但不幸的是它不适合我。这是我第一次与NotificationCenter
工作,并使用此需要时,我想的XLPagerTabStrip一个选项卡下将数据传递到一个视图 - 控制首次出现。通知中心并没有叫我搜索这个,但问题依然存在,我选择
所以在这里是怎么了张贴通知:
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)
不会被调用。
UPDATE
类,这是张贴通知是ViewController.swift
并具有观察者类是AvailableViewController.swift
基于评论我已经改变了观察员NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
并生成该错误。 并做后续我得到了同样的错误。
Value of type 'AvailableViewController' has no member 'gotDocID'
您可以使用下面的代码发布和获取数据。
//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
}
}
我得到'Type'AvailableViewController'没有成员'gotDocID'',我使用的是swift 3.2,所以'NSNotificationCenter'是'NotificationCenter',我已经根据你的代码进行了转换,但仍然出现这个错误。您也可以在此问题的更新部分的屏幕截图中看到此错误。 –
@ChaudhryTalha使用self.gotDocID(_ :)而不是AvailableViewController.gotDocID(notification :)并且它将起作用。我检查这是工作 –
添加@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);
我不知道为什么,但我得到这个错误:类型'AvailableViewController'没有成员'gotDocID(notification:)' –
我测试了代码,它没有给任何错误集游标'#selector(AvailableViewController.')后检查自动完成中的func – Mukesh
你为什么不尝试关闭
确保您的帖子通知occures。
变化
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>)
}
请检查:
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)
}
}
}
什么mainDoctorsArray的nullcheck结果[sender.tag] .doctors_id? –
你在哪里进行观察,同一类或其他一些类? – karthikeyan
@karthikeyan在其他一些类 –