Llamar a la función externa usando WatchKit force touch MenuItem
Necesito implementar un WatchKit Force-TouchMenuItem
llamar a unsaveWorkout()
método que se encuentra en una clase separada que no subclaseWKInterfaceController
.
Me doy cuenta de que cada clase necesita al menos un inicializador designado. Supongo que esta es la clave?
Por cierto, mi "saveSession () alcanzado"la declaración de impresión se registra en la consola cuando se usa el sim pero no cuando uso un dispositivo. Todas las demás declaraciones de impresión se registran en la consola incluso cuando se usa el dispositivo. Un poco extraño.
Mis intentos de inicialización arrojan varios errores como:
1. error grave: uso del inicializador no implementado 'init ()' para la clase 'DashboardController'
2. Argumento faltante para el parámetro 'contexto' en la llamada
Dashboard.swift
class DashboardController: WKInterfaceController {
@IBOutlet var timerLabel: WKInterfaceTimer!
@IBOutlet weak var milesLabel: WKInterfaceLabel!
// var wSM: WorkoutSessionManager
//init(wSM: WorkoutSessionManager) {
// self.wSM = wSM
// super.init()
// }
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
addMenuItemWithItemIcon(.Accept, title: "Save", action: #selector(DashboardController.saveSession))
}
override func willActivate() {
super.willActivate()
print("Dashboard controller reached")
}
func saveSession() {
//wSM.saveWorkout()
print("saveSession() reached")
}
WorkoutSessionManager.swift
class WorkoutSessionContext {
let healthStore: HKHealthStore
let activityType: HKWorkoutActivityType
let locationType: HKWorkoutSessionLocationType
init(healthStore: HKHealthStore, activityType: HKWorkoutActivityType = .Other, locationType: HKWorkoutSessionLocationType = .Unknown) {
self.healthStore = healthStore
self.activityType = activityType
self.locationType = locationType
}
}
protocol WorkoutSessionManagerDelegate: class {
// ... protocol methods
}
class WorkoutSessionManager: NSObject, HKWorkoutSessionDelegate {
let healthStore: HKHealthStore
let workoutSession: HKWorkoutSession
init(context: WorkoutSessionContext) {
self.healthStore = context.healthStore
self.workoutSession = HKWorkoutSession(activityType: context.activityType, locationType: context.locationType)
self.currentActiveEnergyQuantity = HKQuantity(unit: self.energyUnit, doubleValue: 0.0)
self.currentDistanceQuantity = HKQuantity(unit: self.distanceUnit, doubleValue: 0.0)
super.init()
self.workoutSession.delegate = self
}
func saveWorkout() {
guard let startDate = self.workoutStartDate, endDate = self.workoutEndDate else {return}
// ...code...