如何在 WatchOS 的基于页面的界面中显示警报?
Posted
技术标签:
【中文标题】如何在 WatchOS 的基于页面的界面中显示警报?【英文标题】:How to display an alert in page based interface on WatchOS? 【发布时间】:2019-09-22 19:40:32 【问题描述】:我的手表应用程序有一个基于页面的界面,我真的不知道当前屏幕上的 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])
【问题讨论】:
【参考方案1】:您可以使用共享WKExtesion
对象中的visibileInterfaceController
属性;
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])
【讨论】:
以上是关于如何在 WatchOS 的基于页面的界面中显示警报?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 watchOS 上基于分页的界面中的多个界面控制器之间共享变量
检测到 WatchOS 7.1 WKLongPressGesture,显示警报,但未触发操作关闭