2017-09-27 66 views
0

。我發現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(notific‌​ation:)), name: Notification.Name("NotificationIdentifier"), object: nil)並生成該錯誤。 enter image description here 並做後續我得到了同樣的錯誤。 enter image description here

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

+0

什麼mainDoctorsArray的nullcheck結果[sender.tag] .doctors_id? –

+0

你在哪裏進行觀察,同一類或其他一些類? – karthikeyan

+0

@karthikeyan在其他一些類 –

回答

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 
    } 
} 
+0

我得到'Type'AvailableViewController'沒有成員'gotDocID'',我使用的是swift 3.2,所以'NSNotificationCenter'是'NotificationCenter',我已經根據你的代碼進行了轉換,但仍然出現這個錯誤。您也可以在此問題的更新部分的屏幕截圖中看到此錯誤。 –

+0

@ChaudhryTalha使用self.gotDocID(_ :)而不是AvailableViewController.gotDocID(notification :)並且它將起作用。我檢查這是工作 –

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); 
+0

我不知道爲什麼,但我得到這個錯誤:類型'AvailableViewController'沒有成員'gotDocID(notification:)' –

+0

我測試了代碼,它沒有給任何錯誤集遊標'#selector(AvailableViewController.')後檢查自動完成中的func – Mukesh

1

你爲什麼不嘗試關閉

確保您的帖子通知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>) 

     } 
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) 
     } 
    } 
} 
相關問題