HKWorkoutSessionManager 协议方法未被调用

HKWorkoutSessionManager protocol methods not getting called

我已遵守我的 WorkoutSessionManagerDelegate 并尝试使用这些协议方法更新 UI 但更新功能中没有任何内容被打印或显示。

我的设置需要使用 context 进行初始化,我需要添加代码以在 context 最终更改时更新 WorkoutSessionManager

context 最终发生变化时,我应该如何更新 WorkoutSessionManager

DashboardController.swift

class DashboardController: WKInterfaceController, WorkoutSessionManagerDelegate {

// IBOutlets

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    addMenuItemWithItemIcon(.Accept, title: "Save", action: #selector(DashboardController.saveSession))

    if context is WorkoutSessionContext {

   WorkoutSessionManager.sharedManager(context as! WorkoutSessionContext)

    } else {
        print("Context is not WorkoutSessionContext")
    }
}

func saveSession() {
    WorkoutSessionManager.sharedManager!.stopWorkoutAndSave()
}

func workoutSessionManager(workoutSessionManager: WorkoutSessionManager, didUpdateActiveEnergyQuantity activeEnergyQuantity: HKQuantity) {

    // nothing in this function is getting printed or displayed
    caloriesLabel.setText("\((activeEnergyQuantity.doubleValueForUnit(workoutSessionManager.energyUnit)))")
    print("\(WorkoutSessionManager.sharedManager?.energyUnit)")
    print("testing print line")

}

WorkoutsController.swift

@IBAction func startWalkingButton() {
    print("Walking start button pressed")
    presentControllerWithName("Dashboard", context: sessionContext)
    WorkoutSessionManager.sharedManager!.startWorkout(.WalkingButton)   
// no code-completion
}

您从未调用过该函数,为了能够调用该函数,您必须具有对 DashboardController 的引用,以便您在 func startWalkingButton() 中使用。由于这是我们上面讨论的 Watch Kit,我不知道最好的方法,但这里有 Apples 示例代码:https://developer.apple.com/library/ios/samplecode/WKInterfaceCatalog/Introduction/Intro.html 将指导您如何正确地做到这一点。如果您在执行此操作时需要帮助,请提出您现在遇到的更具体的问题。