2016-02-12 90 views
1

我正在做Apple Watch App,其中并发症对`ComplicationController'使用`InterfaceController`逻辑

我已经得到了WatchKit应用部分与该Ev类工作的伟大 ...

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

} 

我有一个硬在我的Complication中获得同样的东西,任何想法?

我不确定我是否可以对我的ExtensionDelegate使用相同的Ev代码,然后确定要放入我的ComplicationController

如果我用我的ExtensionDelegate我得到一个致命错误相同Ev代码:使用未实现初始化的init()的。

在我ComplicationController我不知道如何去使用最好的我已经从InterfaceController有数据ComplicationController填写getCurrentTimelineEntryForComplication & getTimelineEntriesForComplication方法。

将根据需要发布任何额外的代码,谢谢!

编辑: 每一个问题,我的数据来自CloudKit的iPhone应用程序(我然后通过WCSession传递到监视应用程序,所以我的问题是访问在我的收藏应用程序的数据为我并发症)

+0

你的数据来自哪里?数据库?一个远程API? –

+0

CloudKit将数据提供给iPhone应用程序,然后将该数据传递给Watch应用程序。这部分工作很好,我只需要访问我的手表应用程序的数据,以填补你知道的并发症? (我会在我的问题中注意到CloudKit的一部分) – SRMR

回答

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建立可以参考它的InterfaceControllerComplicationsController

与一个单身的想法是,你有一个全球性的参考。 DataManager只能实例化一次。

+0

就是那种我已经用“Ev”进行过的或者没有? – SRMR

+0

“Ev”似乎只是一个模型对象。你的singleton对象将包含一个'Ev'对象的集合,你可以使用它来构建你的'InterfaceController'以及你的'ComplicationController'。单身人士也会收到'WCSession'消息并创建Ev'对象 –

+0

我知道这是一个非常常见的事情,但我从来没有这样做过,所以如果您有任何关于使用watchOS 2的文章,不胜感激!然后,我可以放弃它。我基本上是想用'ExtensionDelegate'作为一个单身人士,并且很难弄清楚你应该怎样看待你知道的 – SRMR