2015-05-19 9 views
14

Jak wysłać numer i napis do zgłoszenia ...jak przekazać wiele wartości z zawiadomieniem w szybkim

let mynumber=1; 
let mytext="mytext"; 
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????); 

i odbierać wartości w odbiorniku?

func refreshList(notification: NSNotification){ 
     let receivednumber=?????????? 
     let receivedString=????????? 
    } 

Odpowiedz

22

Można owinąć je w NSArray lub NSDictionary lub niestandardowego obiektu.

Np

let mynumber=1; 
let mytext="mytext"; 

let myDict = [ "number": mynumber, "text":mytext] 

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object:myDict); 

func refreshList(notification: NSNotification){ 
    let dict = notification.object as! NSDictionary 
    let receivednumber = dict["number"] 
    let receivedString = dict["mytext"] 
} 
+0

Thanks to działa, ale pojawia się wartość "Opcjonalnie (myText)" dla tekstu. Czy to powinno być i muszę usunąć opcjonalny ciąg znaków lub czy robię coś nie tak. –

+0

@mcflysoft Spójrz na ten temat na temat opcji: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5 -ID330 – Moritz

11

Użyj userInfo

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: ["number":yourNumber, 
           "string":yourString] 

i odzyskać:

func refreshList(notification: NSNotification){ 
    let userInfo = notification.userInfo as Dictionary 
    let receivednumber = userInfo["number"] 
    let receivedString = userInfo["string"] 
} 

Nie jestem mocny w sprawie SWIFT (niesprawdzone), ale masz pomysł.

3

W rzeczywistości jest na to wiele sposobów. Jednym z nich jest, aby przekazać tablicę obiektów, takich jak:

let arrayObject : [AnyObject] = [mynumber,mytext] 

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: arrayObject) 

func refreshList(notification: NSNotification){ 

    let arrayObject = notification.object as! [AnyObject] 

    let receivednumber = arrayObject[0] as! Int 
    let receivedString = arrayObject[1] as! String 
} 
20

Xcode 8.3.1 • Swift 3,1

extension Notification.Name { 
    static let refresh = Notification.Name("refresh") 
} 

let myDict: [String: Any] = ["myInt": 1, "myText": "text"] 
NotificationCenter.default.post(name: .refresh, object: myDict) 

NotificationCenter.default.addObserver(self, selector: #selector(refreshList), name: .refresh, object: nil) 

// don't forget vvv add an underscore before the view controller method parameter 
func refreshList(_ notification: Notification) { 
    if let myDict = notification.object as? [String: Any] { 
     if let myInt = myDict["myInt"] as? Int { 
      print(myInt) 
     } 
     if let myText = myDict["myText"] as? String { 
      print(myText) 
     } 
    } 
} 
+1

Dziękuję za link! – MeV

0

Swift 4.0, przechodzę pojedynczy klucz: wartość, możesz dodać wiele kluczy i wartości.

NotificationCenter.default.post(name:NSNotification.Name(rawValue: "updateLocation"), object: ["location":"India"]) 

Dodawanie obserwatora i sposób definicja

NotificationCenter.default.addObserver(self, selector: #selector(getDataUpdate), name: NSNotification.Name(rawValue: "updateLocation"), object: nil) 

@objc func getDataUpdate(notification: Notification) { 
     guard let object = notification.object as? [String:Any] else { 
      return 
     } 
     let location = object["location"] as? String 
     self.btnCityName.setTitle(location, for: .normal) 

     print(notification.description) 
     print(notification.object ?? "") 
     print(notification.userInfo ?? "") 
    }