我正在使用应用进行Apple Watch 应用。
我已经使用此Ev
课程 WatchKit App部分工作 ...
class Ev {
var evTColor:String
var evMatch:String
init(dataDictionary:Dictionary<String,String>) {
evTColor = dataDictionary["TColor"]!
evMatch = dataDictionary["Match"]!
}
class func newEv(dataDictionary:Dictionary<String,String>) -> Ev {
return Ev(dataDictionary: dataDictionary)
}
}
...以及此InterfaceController
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
if let tColorValue = userInfo["TColor"] as? String, let matchValue = userInfo["Match"] as? String {
receivedData.append(["TColor" : tColorValue , "Match" : matchValue])
evs.append(Ev(dataDictionary: ["TColor" : tColorValue , "Match" : matchValue]))
doTable()
} else {
print("tColorValue and matchValue are not same as dictionary value")
}
}
func doTable() {
self.rowTable.setNumberOfRows(self.evs.count, withRowType: "rows")
for (index, evt) in evs.enumerate() {
if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {
row.mLabel.setText(evt.evMatch)
row.cGroup.setBackgroundColor(colorWithHexString(evt.evTColor))
} else {
print("nope")
}
}
}
我很难在“复杂功能”中使用同样的东西,有什么想法吗?
我不确定我是否可以为我的Ev
使用相同的ExtensionDelegate
代码,然后确切地将ComplicationController
放入Ev
。
如果我在ExtensionDelegate
中使用相同的ComplicationController
代码,我会收到致命错误:使用未实现的初始化程序init()。
在我的InterfaceController
中,我不确定如何最好地使用getCurrentTimelineEntryForComplication
中已有的数据来填写getTimelineEntriesForComplication
&amp; {{1} } ComplicationController
中的方法。
将根据需要发布任何额外的代码,谢谢!
修改
根据一个问题,我的数据来自CloudKit到iPhone应用程序(然后我通过WCSession
传递给Watch App,所以我的问题是在我的应用程序的Watch App中访问该数据)
答案 0 :(得分:3)
我没有让你的InterfaceController
实现并接收WCSession
消息,而是设置一个单例类来接收这些消息。该类可以解析和整理WCSession
中的用户信息数据。您可以/ ComplicationController
和InterfaceController
单身人士很容易在swift中设置:
class DataManager : WCSessionDelegate {
// This is how you create a singleton
static let sharedInstance = DataManager()
override init() {
super.init()
if WCSession.isSupported() {
self.watchConnectivitySession?.delegate = self
self.watchConnectivitySession?.activateSession()
}
}
// This is where you would store your `Ev`s once fetched
var dataObjects = [Ev]()
// This is the method that would fetch them for you
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
//parse your userInfoDictionary
self.dataObjects = evs
}
}
然后在您的InterfaceController
中,您可以使用DataManager.sharedInstance.dataObjects
引用它来构建InterfaceController
或ComplicationsController
单例的想法是你有一个全局引用。 DataManager仅实例化一次且仅实例化一次。