NSNotificationCenter用于刷新不通过addObserver移动的数据

时间:2016-02-19 02:48:58

标签: ios xcode swift watchkit nsnotificationcenter

我需要使用NSNotificationCenter来更新我来自WCSession的新数据,以便我可以填写tableview

我在列出的每个功能中设置了断点,但出于某种原因,正确 NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)然后没有其他

所以有些事情是对的,你能帮助我吗?谢谢!

DataManager

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

    }

InterfaceController

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

1 个答案:

答案 0 :(得分:1)

DataManager:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil, userInfo: [String : AnyObject])

InterfaceController:

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
}