使用watchConnectivity发送变量但无法让watch应用中的选择器进行更新

时间:2017-12-29 11:46:26

标签: iphone swift apple-watch watchconnectivity

我一直试图从iPhone发送一个变量到手表。我已设法使用watchConnectivity发送它但我无法通过监视应用程序使用我发送的新变量更新选择器。

以下是观看应用的代码:

import WatchKit
import Foundation
import WatchConnectivity

var bigDict = ["":""]

class InterfaceController: WKInterfaceController, WCSessionDelegate {

    lazy var keys = Array(bigDict.keys)
    lazy var values = Array(bigDict.values)
    var pickerItems: [WKPickerItem] = []

    @IBOutlet var pickerW: WKInterfacePicker!

    @IBAction func pickerDidChange(_ value: Int) {

    }

    @IBAction func updateButton() {
        for item in keys{
            let pickerItem = WKPickerItem()
            pickerItem.title = item
            pickerItem.caption = bigDict[item]
            pickerItems += [pickerItem]
        }
        pickerW.setItems(pickerItems)
    }

    //func refreshPickerItems() {
        //for item in keys{
            //let pickerItem = WKPickerItem()
            //pickerItem.title = item
            //pickerItem.caption = bigDict[item]
            //pickerItems += [pickerItem]
       // }

        //pickerW.setItems(pickerItems)

   // }


    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        // Configure interface objects here.
        //refreshPickerItems()
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
        if WCSession.isSupported() {
            let session = WCSession.default
            session.delegate = self
            session.activate()
        }
    }

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {

    }

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

        print(message)
        bigDict = message as! [String : String]
            print(bigDict)
    }
}

1 个答案:

答案 0 :(得分:0)

你不应该打电话给" updateButton()"当你收到数据?

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

    print(message)
    bigDict = message as! [String : String]
    updateButton()
    print(bigDict)


}