我正在使用ExtensionDelegate,因此可以从自己的evnts(最终是InterfaceController)访问ComplicationController变量。

当我从evnts 中获取数据时,我需要刷新ExtensionDelegate中的WCSession, didReceiveUserInfo,但是无法弄清楚怎么做,有什么想法吗?

原因是:evnts为空,因为它在WCSession, didReceiveUserInfo运行以实际获取数据之前被调用。

(任何问题都让我知道,并将根据需要发布任何额外的代码!)

ExtensionDelegate:

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    var evnts = [Evnt]()
}

InterfaceController:
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

    if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {

        let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
        var extEvnts = myDelegate.evnts

        receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
        extEvnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))

        doTable()

    } else {
        print("tColorValue and matchValue are not same as dictionary value")
    }

}


func doTable() {

    let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
    let extEvnts = myDelegate.evnts

    self.rowTable.setNumberOfRows(extEvnts.count, withRowType: "rows")

    for (index, evt) in extEvnts.enumerate() {

        if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {

            row.mLabel.setText(evt.eventMatch)
            row.cGroup.setBackgroundColor(colorWithHexString(evt.eventTColor))
        } else {
            print("nope")
        }
    }
}

最佳答案

您可以将evnts中的ExtensionDelegate设为静态变量

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    static var evnts = [Evnt]()
}

然后,您还需要进行更改:
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])
        ExtensionDelegate.evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))

        doTable()

    } else {
        print("tColorValue and matchValue are not same as dictionary value")
    }

}


func doTable() {

    let extEvnts = ExtensionDelegate.evnts

    self.rowTable.setNumberOfRows(extEvnts.count, withRowType: "rows")

    for (index, evt) in extEvnts.enumerate() {

        if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {

            row.mLabel.setText(evt.eventMatch)
            row.cGroup.setBackgroundColor(colorWithHexString(evt.eventTColor))
        } else {
            print("nope")
        }
    }
}

关于ios - 更新扩展委托(delegate)中的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35495347/

10-11 02:43