Ios NSNotificationCenter刷新未经过addObserver的数据
当新数据从Ios NSNotificationCenter刷新未经过addObserver的数据,ios,xcode,swift,watchkit,nsnotificationcenter,Ios,Xcode,Swift,Watchkit,Nsnotificationcenter,当新数据从WCSession传入时,我需要使用NSNotificationCenter更新我,以便填写我的表格视图 我在列出的每个函数上设置断点,但由于某种原因,它会直接转到NSNotificationCenter.defaultCenter().addObserver(self,selector:“loadList:”,name:“load”,object:nil)和,然后就没有其他地方了 有点不对劲,你能帮我吗?谢谢 数据管理器: class DataManager : NSObject,
WCSession
传入时,我需要使用NSNotificationCenter
更新我,以便填写我的表格视图
我在列出的每个函数上设置断点,但由于某种原因,它会直接转到NSNotificationCenter.defaultCenter().addObserver(self,selector:“loadList:”,name:“load”,object:nil)
和,然后就没有其他地方了
有点不对劲,你能帮我吗?谢谢
数据管理器
:
class DataManager : NSObject, WCSessionDelegate {
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {
receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))
self.dataObjects = evnts
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
} else {
print("not same as dictionary value")
}
}
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
}
func loadList(notification: NSNotification){
//load table data here
doTable()
}
func doTable() {
self.rowTable.setNumberOfRows(DataManager.sharedInstance.dataObjects.count, withRowType: "rows")
for (index, evt) in DataManager.sharedInstance.dataObjects.enumerate() {
if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {
row.mLabel.setText(evt.eventMatch)
} else {
print("nope")
}
}
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil, userInfo: [String : AnyObject])
NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)
//........
func doThisWhenNotify(notification : NSNotification) {
let info = notification.userInfo
//load your stuff here
}
接口控制器
:
class DataManager : NSObject, WCSessionDelegate {
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {
receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))
self.dataObjects = evnts
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
} else {
print("not same as dictionary value")
}
}
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
}
func loadList(notification: NSNotification){
//load table data here
doTable()
}
func doTable() {
self.rowTable.setNumberOfRows(DataManager.sharedInstance.dataObjects.count, withRowType: "rows")
for (index, evt) in DataManager.sharedInstance.dataObjects.enumerate() {
if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {
row.mLabel.setText(evt.eventMatch)
} else {
print("nope")
}
}
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil, userInfo: [String : AnyObject])
NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)
//........
func doThisWhenNotify(notification : NSNotification) {
let info = notification.userInfo
//load your stuff here
}
数据管理器:
class DataManager : NSObject, WCSessionDelegate {
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {
receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))
self.dataObjects = evnts
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
} else {
print("not same as dictionary value")
}
}
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
}
func loadList(notification: NSNotification){
//load table data here
doTable()
}
func doTable() {
self.rowTable.setNumberOfRows(DataManager.sharedInstance.dataObjects.count, withRowType: "rows")
for (index, evt) in DataManager.sharedInstance.dataObjects.enumerate() {
if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {
row.mLabel.setText(evt.eventMatch)
} else {
print("nope")
}
}
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil, userInfo: [String : AnyObject])
NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)
//........
func doThisWhenNotify(notification : NSNotification) {
let info = notification.userInfo
//load your stuff here
}
接口控制器:
class DataManager : NSObject, WCSessionDelegate {
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {
receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))
self.dataObjects = evnts
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
} else {
print("not same as dictionary value")
}
}
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
}
func loadList(notification: NSNotification){
//load table data here
doTable()
}
func doTable() {
self.rowTable.setNumberOfRows(DataManager.sharedInstance.dataObjects.count, withRowType: "rows")
for (index, evt) in DataManager.sharedInstance.dataObjects.enumerate() {
if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {
row.mLabel.setText(evt.eventMatch)
} else {
print("nope")
}
}
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil, userInfo: [String : AnyObject])
NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)
//........
func doThisWhenNotify(notification : NSNotification) {
let info = notification.userInfo
//load your stuff here
}
似乎未调用会话:didReceiveUserInfo:
。你能告诉我在你的代码中你在哪里使用数据管理器吗?我同意@thislooksfun,你是如何通过WCSession发送数据的?@thislooksfun是的,这似乎是正在发生的事情。我在上面发布了doTable()
中的DataManager
用法,但是如果您在谈论其他内容,而我不知道您在说什么,请告诉我。将根据需要发布任何额外内容,谢谢@KyleRedfearn我的过程(一定是错误的)是:(DataManager.swift
)->在WCSession
->中获取数据,使用NSNotificationCenter
发出通知,通知数据已通过NSNotificationCenter.defaultCenter().postNotificationName(“load”,object:nil)
->(InterfaceController.swift
)->通过NSNotificationCenter.defaultCenter().addObserver(self,selector:“loadList:,name:“load”,object:nil)侦听并接收数据已从DataManager
加载的通知
它调用…->加载列表:
它只运行doTable
->doTable
加载表。您的流程正确,但需要在它之前添加一些项目。通过WCSession API将数据从应用程序发送到手表