簡體   English   中英

更新擴展委托中的數組

[英]Update array in Extension Delegate

我正在使用ExtensionDelegate因此可以從InterfaceController (最終是ComplicationController )訪問evnts變量。

需要刷新evntsExtensionDelegate當我從獲取數據WCSession, didReceiveUserInfo ,但不能找出任何相當如何想法?

原因是: evnts為空,因為它在WCSession, didReceiveUserInfo之前被調用, WCSession, didReceiveUserInfo運行以實際獲取數據。

(任何問題都讓我知道,並將根據需要發布任何額外的代碼!)

ExtensionDelegate

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    var evnts = [Evnt]()
}

InterfaceController

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

    if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {

        let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
        var extEvnts = myDelegate.evnts

        receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
        extEvnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))

        doTable()

    } else {
        print("tColorValue and matchValue are not same as dictionary value")
    }

}


func doTable() {

    let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
    let extEvnts = myDelegate.evnts

    self.rowTable.setNumberOfRows(extEvnts.count, withRowType: "rows")

    for (index, evt) in extEvnts.enumerate() {

        if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {

            row.mLabel.setText(evt.eventMatch)
            row.cGroup.setBackgroundColor(colorWithHexString(evt.eventTColor)) 
        } else {
            print("nope")
        }
    }    
}

您可以在ExtensionDelegate evnts設為靜態變量

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    static var evnts = [Evnt]()
}

然后,您還需要進行更改:

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

    if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {

        receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
        ExtensionDelegate.evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))

        doTable()

    } else {
        print("tColorValue and matchValue are not same as dictionary value")
    }

}

func doTable() {

    let extEvnts = ExtensionDelegate.evnts

    self.rowTable.setNumberOfRows(extEvnts.count, withRowType: "rows")

    for (index, evt) in extEvnts.enumerate() {

        if let row = rowTable.rowControllerAtIndex(index) as? TableRowController {

            row.mLabel.setText(evt.eventMatch)
            row.cGroup.setBackgroundColor(colorWithHexString(evt.eventTColor)) 
        } else {
            print("nope")
        }
    }    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM