【问题标题】:HKWorkoutSessionManager protocol methods not getting called [closed]HKWorkoutSession Manager 协议方法没有被调用[关闭]
【发布时间】:2016-08-13 19:26:50
【问题描述】:

我已遵守我的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
}

【问题讨论】:

  • 要创建一个真正的单例,初始化必须是私有的。这也可以帮助您找到代码中的非法初始化。
  • 您是否检查过该函数是否实际被调用? (以断点为例)
  • 没有。我的意思是一个断点,看看它是否被调用。如果您的最后一次 print() 调用没有打印到控制台而不是根本没有被调用,并且问题不在您在此处显示的代码中,而是在调用该函数的代码中。
  • 向我显示您期望从哪里调用锻炼会话管理器(workoutSessionManager: WorkoutSessionManager, didUpdateActiveEnergyQuantity activeEnergyQuantity: HKQuantity)的代码
  • 你必须在你想要调用它的地方调用 func锻炼会话管理器(workoutSessionManager: WorkoutSessionManager, didUpdateActiveEnergyQuantity activeEnergyQuantity: HKQuantity)。目前没有调用该函数。

标签: swift watchkit healthkit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多