ios 如何快速通过通知传递多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30328452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass multiple values with a notification in swift
提问by mcfly soft
How to send a number and a String through a notification ...
如何通过通知发送数字和字符串...
let mynumber=1;
let mytext="mytext";
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????);
and receive the values in the receiver ?
并在接收器中接收值?
func refreshList(notification: NSNotification){
let receivednumber=??????????
let receivedString=?????????
}
回答by Pfitz
You could wrap them in an NSArray
or a NSDictionary
or a custom Object.
您可以将它们包装在一个NSArray
或一个NSDictionary
或一个自定义对象中。
Eg:
例如:
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"]
}
回答by Leo Dabus
Xcode 8.3.1 ? Swift 3.1
Xcode 8.3.1 ? 斯威夫特 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)
}
}
}
回答by Eager Logic
You can use the userInfo
property of Notification
:
您可以使用以下userInfo
属性Notification
:
NotificationCenter.default.post(name: Notification.Name("refresh"),
object: nil,
userInfo: ["number":yourNumber, "string":yourString])
and to retrieve:
并检索:
func refreshList(notification: Notification){
let receivednumber = notification.userInfo?["number"] as? Int ?? 0
let receivedString = notification.userInfo?["string"] as? String ?? ""
}
回答by Zell B.
Actually there are a lot of way to do this. One of them is to pass an array of objects like :
实际上有很多方法可以做到这一点。其中之一是传递一组对象,例如:
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
}
回答by Deep
Swift 4.0
斯威夫特 4.0
First create a dictionary for multiple values.
首先为多个值创建一个字典。
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)
Add observer in other ViewController
在其他 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"])
}
回答by Gurjinder Singh
Swift 4.0, I am passing single key:value, you can add multiple keys and values.
Swift 4.0,我传递的是单个键:值,您可以添加多个键和值。
NotificationCenter.default.post(name:NSNotification.Name(rawValue: "updateLocation"), object: ["location":"India"])
Adding Observer and Method definition. You also need to remove observer.
添加观察者和方法定义。您还需要删除观察者。
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 ?? "")
}