【发布时间】: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)。目前没有调用该函数。