。我发现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'
什么mainDoctorsArray的nullcheck结果[sender.tag] .doctors_id? –
你在哪里进行观察,同一类或其他一些类? – karthikeyan
@karthikeyan在其他一些类 –