调用ExtensionDelegate为Complication创建/刷新数据 [英] Call ExtensionDelegate to create/refresh data for Complication
问题描述
我的所有数据创建都在 ExtensionDelegate.swift
中完成。
All my data creation is done in the ExtensionDelegate.swift
.
问题是 ExtensionDelegate.swift
在函数 getCurrentTimelineEntryForComplication
在我的 ComplicationController.swift
。
The problem is ExtensionDelegate.swift
doesn't get called before the function getCurrentTimelineEntryForComplication
in my ComplicationController.swift
.
有什么想法吗? 以下是我的代码和详细信息:
所以我的数组 extEvnts
在我的 ComplicationController.swift
:
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
let extEvnts = ExtensionDelegate.evnts
}
因为我的 ExtensionDelegate.swift
还没有被调用,这就是为数组创建数据的原因:
Because my ExtensionDelegate.swift
hasn't gotten called yet, which is what creates the data for the array:
class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
private let session = WCSession.defaultSession()
var receivedData = Array<Dictionary<String, String>>()
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]))
} else {
print("tColorValue and matchValue are not same as dictionary value")
}
}
func applicationDidFinishLaunching() {
// Perform any final initialization of your application.
if WCSession.isSupported() {
session.delegate = self
session.activateSession()
}
}
}
编辑:
每个Apple,看起来这与它有关,但由于某种原因我不知道如何实际实现它,因为我无法调用 mydelegate.evnts
:
Per Apple, it looks like this has something to do with it, but for some reason I have no idea how to actually implement it because I'm not able to call mydelegate.evnts
:
// Get the complication data from the extension delegate.
let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
var data : Dictionary = myDelegate.myComplicationData[ComplicationCurrentEntry]!
所以我尝试了类似这样的东西,但仍然无法使它工作,因为我是仍然没有数据:
So I've tried something like this, and still can't get it working because I'm still getting no data:
func someMethod() {
let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
let dict = ExtensionDelegate.evnts
print("ExtensionDel.evnts: \(dict.count)")
}
推荐答案
帮助我的有用问题在其他地方
在函数 requestedUpdateDidBegin()
中,您可以更新将在并发症中显示的信息。因此,在此方法中,您可以使用<$ h $ => https://developer.apple.com/library/prerelease/ WatchConnectivity
方法调用您的父应用。 ios / documentation / WatchConnectivity / Reference / WCSession_class / index.htmlrel =nofollow noreferrer> sendMessage:replyHandler:errorHandler:以接收新信息。
In the function requestedUpdateDidBegin()
you can update the information that you will display in your complication. So in this method you may make a call to your parent app using a WatchConnectivity
method like sendMessage:replyHandler:errorHandler: to receive new information.
您可以使用 NSUserDefaults
来存储将在 ComplicationController
中使用的命令性数据,然后从<您的并发症需要code> NSUserDefaults 。我将这些数据存储在用户默认值中,以便在新数据无法加载时始终显示旧数据。
You can use NSUserDefaults
to store your imperative data that will be used in your ComplicationController
, then load this information from NSUserDefaults
for your complication. I store this data in user defaults so that I always have old data to display in case the new data fails to load.
这篇关于调用ExtensionDelegate为Complication创建/刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!