2017-09-27 6 views
0

Ich habe darüber gesucht, aber das Problem existiert immer noch für mich. Ich fand this great question aber leider hat es nicht für mich funktioniert. Dies ist das erste Mal, dass ich mit NotificationCenter arbeite und die Notwendigkeit, diese zuerst zu verwenden, tritt auf, wenn ich Daten an einen Viewcontroller unter einer Registerkarte XLPagerTabStrip übergeben wollte.NotificationCenter ruft den Selektor nicht an

Also hier ist, wie ich die Mitteilung bin Entsendung:

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

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

In der Klasse I für die Beobachtung dieser Benachrichtigung gemacht habe ich NotificationCenter.default.addObserver(self, selector: #selector(gotDocID), name: Notification.Name("docID"), object: nil)

Der Selektor Methode bin Aufruf ist:

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) 
    } 
} 

Ich habe auch versucht, Beobachter hinzufügen als: NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notification:)), name: Notification.Name("docID"), object: nil) aber immer noch das gleiche Ergebnis.

Das Problem ist, dass func gotDocID(notification:NSNotification) nicht aufgerufen wird.

UPDATE

Klasse, die die Benachrichtigung Entsendung ist ViewController.swift und die Klasse, die den Betrachter hat, ist AvailableViewController.swift

auf einen Kommentar Basierend I Beobachter NotificationCenter.default.addObserver(self, selector: #selector(AvailableViewController.gotDocID(notific‌​ation:)), name: Notification.Name("NotificationIdentifier"), object: nil) und dieser Fehler geändert haben wird erzeugt. enter image description here und auch durch die folgende Folge bekomme ich den gleichen Fehler. enter image description here

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

+0

was ist das nullcheck Ergebnis mainDoctorsArray [sender.tag] .doctors_id? –

+0

wo beobachtest du dieselbe Klasse oder eine andere Klasse? – karthikeyan

+0

@karthikeyan in einer anderen Klasse –

Antwort

1

Sie können den Code unten verwenden, um Daten zu veröffentlichen und zu erhalten.

//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

Ich bekomme 'Type 'AvailableViewController' hat kein Mitglied 'gotDocID'' und ich benutze swift 3.2.' NSNotificationCenter' ist also' NotificationCenter' welches ich nach deinem Code konvertiert habe, aber immer noch diesen Fehler. Sie können diesen Fehler auch im Screenshot im Update-Abschnitt dieser Frage sehen. –

+0

@ChaudhryTalha Verwenden Sie self.gotDocID (_ :) anstelle von AvailableViewController.gotDocID (Benachrichtigung :) und es wird funktionieren. Ich habe überprüft, das funktioniert –

1

@objc auf Ihre Funktion

@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

Ich weiß nicht warum, aber ich bekomme diesen Fehler: Typ 'AvailableViewController 'hat kein Mitglied' gotDocID (notification:)' –

+0

Ich habe den Code getestet, es gibt keine Fehler gesetzt Cursor Nach '#selector (AvailableViewController.' und prüfen Sie auf Funktion in Autocomplete – Mukesh

1

hinzufügen Warum Sie Schließung

sicher nicht versuchen Sie Ihr Beitrag Benachrichtigung occures.

ändern

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

Bitte überprüfen Sie:

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) 
     } 
    } 
} 
Verwandte Themen