如何在 swift 中通过通知传递多个值

how to pass multiple values with a notification in swift

如何通过通知发送数字和字符串...

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

并在接收器中接收值 ?

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

您可以使用 NotificationuserInfo 属性:

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 ?? ""
}

其实有很多方法可以做到这一点。其中之一是传递一组对象,例如:

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
}

您可以将它们包装在 NSArrayNSDictionary 或自定义对象中。

例如:

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

Swift 4 或更高版本

声明要使用的通知名称

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

在您的视图控制器 viewDidLoad 方法中为该名称添加一个观察者,并添加一个选择器以获取通知对象

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

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

Post 你的对象通知:

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

Swift 4.0,我传单key:value,你可以添加多个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 ?? "")
    }

Swift 4.0

首先为多个值创建字典。

let name = "Abhi"
let age = 21
let email = "abhi@xyz.com"
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"])

}