0

我的手表应用程序有一个基于页面的界面,我真的不知道屏幕上当前是什么 InterfaceController(它可能是 4 个中的 1 个),但是无论应用程序在哪个屏幕上,我都需要弹出警报。鉴于我不一定知道哪个 InterfaceController 是“当前的”,我如何显示警报?下面的代码只有在用户导航到这个 InterfaceController 时才会运行。如果用户不在该页面上,那么我会在控制台中看到此错误2019-09-22 15:42:01.597663-0400 Watch Extension[501:526217] Warning: Attempt to present <PUICAlertSheetController: 0x18158c00> on <SPInterfaceViewController: 0x1795e800> whose view is not in the window hierarchy!

extension WorkoutControlsInterfaceController: WorkoutEndedDelegate {
    func timerEndedCheckToSeeIfWorkoutEnded(_ manager: WorkoutManager) {
        let endWorkoutAction = WKAlertAction(title: "End Workout", style: .default, handler: {
            print("User has selected to end the workout")
            self.workoutManager?.stopWorkout()

        })
        let cancelAction = WKAlertAction(title: "Cancel", style: .cancel, handler: {

        })
        self.becomeCurrentPage()
        self.presentAlert(withTitle: "Workout Ended?", message: "It looks like your workout may have ended?", preferredStyle: .alert, actions: [endWorkoutAction, cancelAction])
    }


}
4

1 回答 1

1

您可以使用visibileInterfaceController共享WKExtesion对象中的属性;

extension WorkoutControlsInterfaceController: WorkoutEndedDelegate {
    func timerEndedCheckToSeeIfWorkoutEnded(_ manager: WorkoutManager) {
        let endWorkoutAction = WKAlertAction(title: "End Workout", style: .default, handler: {
            print("User has selected to end the workout")
            self.workoutManager?.stopWorkout()

        })
        let cancelAction = WKAlertAction(title: "Cancel", style: .cancel, handler: {

        })

        WKExtension.shared().visibleInterfaceController?.presentAlert(withTitle: "Workout Ended?", message: "It looks like your workout may have ended?", preferredStyle: .alert, actions: [endWorkoutAction, cancelAction])
    }


}
于 2019-09-22T20:01:34.353 回答