【问题标题】:WatchOS 7.1 WKLongPressGesture detected, alert presented, but action closures not firing检测到 WatchOS 7.1 WKLongPressGesture,显示警报,但未触发操作关闭
【发布时间】:2020-11-28 16:54:06
【问题描述】:

我正在尝试在我的应用中实现 WKLongPressGestureRecognizer。长按手势被识别并呈现警报。但是,当我选择清除表格的选项(我正在使用 Realm)时,警报控制器被解除,但未清除拍摄。当我尝试通过在代码中添加断点进行调试时,似乎完全跳过了闭包内的代码。知道我错过了什么吗? (我应该使用警报表的操作表吗?)我尝试了很多不同的东西。这是我一直用来调试的带有打印语句的代码。当前没有触发闭包内的打印语句。

@IBAction func handleLongPress(_ sender: Any) {

print("long press pressed")

WKInterfaceDevice.current().play(.click)

let clearAction = WKAlertAction(title: "Clear", style: .destructive) {
    print("clear button pressed")
}

let cancelAction = WKAlertAction(title: "Cancel", style: .default) {
    print("cancel button pressed")
}

presentAlert(withTitle: "Are you sure?", message: "Action cannot be undone", preferredStyle: .alert, actions: [clearAction, cancelAction])

print("exiting long press")}

感谢您的任何意见或建议。

【问题讨论】:

    标签: xcode watchkit apple-watch


    【解决方案1】:

    手势识别器将调用选择器handleLongPress 两次,一次使用状态== began,一次使用状态cancelled。我发现这会导致您描述的 presentAlert 出现问题。

    尝试检查began 状态:

    @IBAction func handleLongPress(_ sender: WKGestureRecognizer) {
        guard sender.state == .began else {
            return
        }
        // rest of handleLongPress implementation goes here
    }
    

    这应该确保您的警报只显示一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      相关资源
      最近更新 更多