如何通过通知发送串号及一个...
let mynumber=1;
let mytext="mytext";
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????);
并接收在接收器中的值?
func refreshList(notification: NSNotification){
let receivednumber=??????????
let receivedString=?????????
}
你可以在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"]
}
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)
}
}
}
您可以使用userInfo
的Notification
属性:
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
}
雨燕4.0
首先创建一个字典多个值。
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"])
}
雨燕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 ?? "")
}