WatchKit Complication:从扩展委托获取Complication数据

我有我的WatchKit扩展中需要的所有数据(从iOS应用程序传递)。

我使用WatchKit InterfaceController的数据来填充表格,该表格完美地工作。

我试图找出在WatchKit ComplicationController获取相同数据的最佳方法。

目前,在InterfaceController ,使用didReceiveUserInfo传入数据:

 func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { if let beachValue = userInfo["Surf"] as? String { places.append(Place(dataDictionary: ["Surf" : surfValue])) } else { print("Something went wrong") } } 

我是否需要在我的ComplicationController调用这个相同的WCSession方法,并再次执行整个数据抓取, 还是有更简单的方法让我访问相同的数据以便在ComplicationController使用?

任何帮助赞赏。 谢谢!

编辑

我的表function:

 func makeTable() { // Per SO let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate let accessVar = myDelegate.places self.rowTable.setNumberOfRows(accessVar.count, withRowType: "rows") for (index, evt) in accessVar.enumerate() { if let row = rowTable.rowControllerAtIndex(index) as? TableRowController { row.mLabel.setText(evt.evMat) } else { print(“No”) } } } 

 // Get the complication data from the extension delegate. let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate var data : Dictionary = myDelegate.myComplicationData[ComplicationCurrentEntry]! 

以上从苹果公司的文件只是一个例子,在您的扩展代理存储你的复杂的需要的数据,看你如何可以轻松地访问它作为一个单身。 对“myComplicationData”的引用是Dictionary的一个示例,默认情况下不是ExtensionDelegate中的参数。

您可以将自己的课程设置为一个单独的类,它可以保存手表的数据,如下所示:

 // Access by calling: // Model.sharedModel.modelVal1 class Model { static let sharedModel = Model() var modelVal1: Float! var modelVal2: String! } 

或者使用扩展委托作为你的单例,并像下面一样将你的属性添加到它的类。 这将允许您访问您在ExtensionDelegate中创build的任何variables。

 // ExtensionDelegate.swift class ExtensionDelegate: NSObject, WKExtensionDelegate { var dataVar1: Float! var dataVar2: String! var myDictionary: [String: String]! } // ComplicationController.swift import WatchKit class ComplicationController: NSObject, CLKComplicationDataSource { func someMethod() { let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate // Here is an example of accessing the Float variable let accessVar = myDelegate.dataVar1 let myDict = myDelegate.myDictionary } } 

无论使用哪种方式,都有助于将数据保存在一个地方,以便您可以随时从手表扩展程序中的任何课程中访问它。

那么,我在我的应用程序做了什么是build立另一个singelton类负责提取和保存我的Watch应用程序和复杂的数据。 但是这对我来说不是最好的方式。 不幸的是我没有得到苹果的代码

 var data : Dictionary = myDelegate.myComplicationData[ComplicationCurrentEntry]! 

在所有。 我不明白这个myComplicationData来自哪里。