gpt4 book ai didi

ios - 更新扩展委托(delegate)中的数组

转载 作者:行者123 更新时间:2023-11-28 06:48:47 24 4
gpt4 key购买 nike

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

当我从 WCSession, didReceiveUserInfo 获取数据时,我需要刷新 ExtensionDelegate 中的 evnts,但是不能不太明白怎么做,有什么想法吗?

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

(如有任何问题请告诉我,并将根据需要发布任何额外代码!)

ExtensionDelegate:

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

接口(interface) Controller :

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

最佳答案

你可以让 ExtensionDelegate 中的 evnts 成为静态变量

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com