加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – WatchKit复杂功能:从扩展委托中获取复杂数据

发布时间:2020-12-15 01:49:06 所属栏目:百科 来源:网络整理
导读:我在WatchKit扩展中拥有所需的所有数据(从iOS应用程序传递). 我使用WatchKit InterfaceController中的数据来填充表格,该表格完美无缺. 我正试图找出在WatchKit ComplicationController中获取相同数据的最佳方法. 目前,在InterfaceController中,使用didReceiv
我在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中使用?

任何帮助赞赏.谢谢!

编辑:

我的表功能:

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

从Apple’s Doc开始,只是在扩展委托中存储复杂化所需数据的一个示例,了解如何以单例形式轻松访问它.对“myComplicationData”的引用是Dictionary的示例,默认情况下不是ExtensionDelegate中的参数.

要么将您自己的类设置为单例,它会为您的手表保存数据,如下所示:

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

或者使用扩展委托作为您的单身人士,并将您的属性添加到其类中,如下所示.这将允许您访问在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
    }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读