HKWorkoutSessionManager协议方法未被调用

时间:2016-08-13 19:26:50

标签: swift watchkit health-kit

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

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

WorkoutSessionManager最终更改时,我应该如何更新context

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
}

1 个答案:

答案 0 :(得分:-1)

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