。我發現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在其他一些類 –