为`ComplicationController'

时间:2016-02-12 01:31:01

标签: ios xcode swift watchkit watch-os-2

我正在使用应用进行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中访问该数据)

1 个答案:

答案 0 :(得分:3)

我没有让你的InterfaceController实现并接收WCSession消息,而是设置一个单例类来接收这些消息。该类可以解析和整理WCSession中的用户信息数据。您可以/ ComplicationControllerInterfaceController

访问该单身类

单身人士很容易在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引用它来构建InterfaceControllerComplicationsController

单例的想法是你有一个全局引用。 DataManager仅实例化一次且仅实例化一次。