web-dev-qa-db-ja.com

swiftの通知で複数の値を渡す方法

通知を介して数字と文字列を送信する方法...

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

受信機で値を受信しますか?

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

NSArrayまたはNSDictionaryまたはカスタムオブジェクトでラップできます。

例えば:

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"]
}
30
Pfitz

Xcode 8.3.1•Swift 3.1

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

let object: [String: Any] = ["id": 1, "email": "[email protected]"]
NotificationCenter.default.post(name: .refresh, object: object)

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

// Swift 4 or later note: add @objc to the selector `@objc func ...`
// don't forget  vvv add an underscore before the view controller method parameter 
func refreshList(_ notification: Notification) {
    if let object = notification.object as? [String: Any] {
        if let id = object["id"] as? Int {
            print(id)
        }
        if let email = object["email"] as? String {
            print(email)
        }
    }
}
24
Leo Dabus

userInfoNotificationプロパティを使用できます。

NotificationCenter.default.post(name: Notification.Name("refresh"),
                                object: nil,
                                userInfo: ["number":yourNumber, "string":yourString])

取得するには:

func refreshList(notification: Notification){
    let receivednumber = notification.userInfo?["number"] as? Int ?? 0
    let receivedString = notification.userInfo?["string"] as? String ?? ""
}
14
Eager Logic

実際、これを行う方法はたくさんあります。それらの1つは、次のようなオブジェクトの配列を渡すことです。

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
}
4
Zell B.

Swift 4.

最初に、複数の値の辞書を作成します。

let name = "Abhi"
let age = 21
let email = "[email protected]"
let myDict = [ "name": name, "age":age, "email":email]
// post myDict
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "post"), object: nil, userInfo: myDict)

他のViewControllerにオブザーバーを追加します

NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "post"), object: nil)

func doThisWhenNotify(notification : NSNotification) {
    let info = notification.userInfo
    print("name : ",info["name"])
    print("age : ",info["age"])
    print("email : ",info["email"])

}
2
Deep

Swift 4.0、単一のkey:valueを渡します。複数のキーと値を追加できます。

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

オブザーバーとメソッド定義を追加します。また、オブザーバーを削除する必要があります。

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 ?? "")
    }
1
Gurjinder Singh