2017-09-27 10 views
0

私はこれについて検索しましたが、問題はまだあります。私はthis great questionを見つけましたが、残念ながらそれは私のためには機能しませんでした。私がNotificationCenterと作業しているのはこれが初めてで、これを最初に使用する必要があるのは、ビューコントローラにデータを渡したいときに、XLPagerTabStripのタブの下に表示されます。私はNotificationCenter.default.addObserver(self, selector: #selector(gotDocID), name: Notification.Name("docID"), object: nil)NotificationCenterがセレクタを呼び出さない

セレクタメソッドを呼んでいるこの通知を観察するために作ったクラスで

if let doc_ID = mainDoctorsArray[sender.tag].doctors_id { 

      NotificationCenter.default.post(name: Notification.Name("docID"), object: nil, userInfo: ["value" : doc_ID]) 
     } 

されています:だからここ

は、私は通知を掲示しています方法です

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あり、観察者を持つクラスは、私はNotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notific‌​ation:)), name: Notification.Name("NotificationIdentifier"), object: nil)にオブザーバーを変更したと、このエラーが生成されたコメントに基づいてAvailableViewController.swift

です。 enter image description here また、私も同じエラーが表示されます。あなたがデータを投稿して取得するために以下のコードを使用することができます enter image description here

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

+0

を.doctors_id [sender.tag] mainDoctorsArrayのnullcheck結果は何ですか? –

+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'は私のコードごとに変換したが、まだこのエラーです。また、この質問の更新セクションのスクリーンショットでこのエラーを見ることができます。 –

+0

@ChaudhryTalha AvailableViewController.gotDocID(notific ation :)の代わりにself.gotDocID(_ :)を使用すると動作します。私はこれが動作していることを確認しています –

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(notific ation:)'というメンバーはありません –

+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) 
     } 
    } 
} 
関連する問題