【问题标题】:how to pass multiple values with a notification in swift如何在swift中通过通知传递多个值
【发布时间】:2015-05-19 14:24:57
【问题描述】:

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

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

并在接收器中接收值?

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

【问题讨论】:

    标签: ios swift notifications


    【解决方案1】:

    您可以将它们包装在 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"]
    }
    

    【讨论】:

    【解决方案2】:

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

    用你的对象发布你的通知:

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

    【讨论】:

      【解决方案3】:

      您可以使用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 ?? ""
      }
      

      【讨论】:

        【解决方案4】:

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

        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
        }
        

        【讨论】:

          【解决方案5】:

          Swift 4.0,我传的是单键:值,你可以添加多个键和值。

             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 是一种类型推断语言post(name: .init(rawValue: "updateLocation")
          【解决方案6】:

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

          【讨论】:

          • Swift 是一种类型推断语言post(name: .init(rawValue: "post")
          猜你喜欢
          • 1970-01-01
          • 2017-12-01
          • 2020-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-27
          • 2020-09-04
          相关资源
          最近更新 更多