WatchKit Complication:从扩展委托获取复杂数据 [英] WatchKit Complication: get Complication data from extension delegate

查看:127
本文介绍了WatchKit Complication:从扩展委托获取复杂数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有WatchKit Extension所需的所有数据(从iOS应用程序传递)。



我使用了数据WatchKit InterfaceController 来填写表格,它完美无缺。



我正试图找出最好的方法在我的WatchKit中获取相同的数据 ComplicationController



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

  func session(session:WCSession,didReceiveUserInfo userInfo:[String:AnyObject]){

如果让beachValue = userInfo [Surf]为?字符串{

places.append(Place(dataDictionary:[Surf:surfValue]))

} else {
print(出错了)
}

}

我是否需要调用它我的 ComplicationController 中的 WCSession 方法再次抓取整个数据,或者是否更容易我可以访问 ComplicationController 吗?

任何帮助赞赏。谢谢!



编辑



我的表格功能:

  func makeTable(){

//每个SO
让myDelegate = WKExtension.sharedExtension()。委托如! ExtensionDelegate
let accessVar = myDelegate.places
self.rowTable.setNumberOfRows(accessVar.count,withRowType:rows)

for accessVar.enumerate中的(index,evt)( ){

如果让row = rowTable.rowControllerAtIndex(index)为? TableRowController {

row.mLabel.setText(evt.evMat)

} else {
print(No)
}
}

}


解决方案

  //从扩展委托中获取并发症数据。 
让myDelegate = WKExtension.sharedExtension()。委托为! ExtensionDelegate
var data:Dictionary = myDelegate.myComplicationData [ComplicationCurrentEntry]!

以上 Apple的文档只是一个示例,用于将您的复杂功能所需的数据存储在您的扩展代理中,了解如何轻松访问它单身人士对myComplicationData的引用是Dictionary的示例,默认情况下不是ExtensionDelegate中的参数。



将您自己的类设置为保存数据的单例对于你的手表如下:

  //通过调用访问:
// Model.sharedModel.modelVal1
class Model {
static let sharedModel = Model()
var modelVal1:Float!
var modelVal2:String!
}

或者使用扩展委托作为您的单身人士,并将您的属性添加到其类中下面。这将允许您访问在ExtensionDelegate中创建的任何变量。

  // ExtensionDelegate.swift 
类ExtensionDelegate: NSObject,WKExtensionDelegate {
var dataVar1:Float!
var dataVar2:String!
var myDictionary:[String:String]!
}


// ComplicationController.swift
import WatchKit

class ComplicationController:NSObject,CLKComplicationDataSource {

func someMethod(){
让myDelegate = WKExtension.sharedExtension()。委托为! ExtensionDelegate
//这是一个访问Float变量的示例
let accessVar = myDelegate.dataVar1
let myDict = myDelegate.myDictionary
}
}

使用任何一种方式都可以帮助您将数据保存在一个位置,这样您就可以随时从手表扩展程序中的任何类访问它。


I have all the data I need in my WatchKit Extension (passed from the iOS app).

I used the data in the WatchKit InterfaceController to fill in a table, which works perfectly.

I'm trying to figure out the best way to get that same data in my WatchKit ComplicationController.

Currently, in the InterfaceController, the data gets passed in using 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")
    }

}

Do I need to call this same WCSession method in my ComplicationController and do the whole data grab again, or is there any easier way for me to access this same data for use in the ComplicationController?

Any help appreciated. Thanks!

EDIT:

My table 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]!

Above from Apple's Doc is just an example on storing the data you need for your complication in your extension delegate seeing as how you can access it easily as a singleton. The reference to "myComplicationData" is an example of a Dictionary and is not a parameter in the ExtensionDelegate by default.

Either set up your own class as a singleton which holds data for your watch like so:

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

Or use the extension delegate as your singleton and add your properties to its class like below. This will allow you to access whatever variables you create in your ExtensionDelegate.

// 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
    }
}

Using either way will help keep your data in one spot so you can always access it from any class in your watch extension.

这篇关于WatchKit Complication:从扩展委托获取复杂数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆