iphone - 使用 watchConnectivity 发送变量但无法让 watch 应用程序中的选择器更新

标签 iphone swift apple-watch watchconnectivity

我一直在尝试从 iPhone 向 watch 发送一个变量。我已设法使用 watchConnectivity 发送它,但我无法让 watch 应用程序中的选择器使用我发送的新变量进行更新。

这是 watch 应用程序的代码:

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

最佳答案

当你收到数据时,你不应该调用“updateButton()”吗?

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

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


}

关于iphone - 使用 watchConnectivity 发送变量但无法让 watch 应用程序中的选择器更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48022145/

相关文章:

iphone - iOS - 每个 View 仅触摸一次

iphone - 相册类控件

ios - 捕获的图像未保存在设备中

swift - 如果它返回 nil,我将如何修复 SKView?

swift - UITabBar isTranslucent 添加了一个额外的 UITabBar?

ios - 在 Apple Watch Storyboard 中的图像顶部添加标签

swift - 当 iOS 处于后台并收到远程通知时向 Watch 发送数据

ios - 无法在 ViewController 的 AppDelegate 中使用数组

swift - 从容器 View 访问导航栏

iphone - 将数据从 Apple Watch 共享到 iPhone