English 中文(简体)
快速倡议警告:在不处于积极状态的情况下,`Attempting - [UIContextMenuInteraction disMenu]
原标题:SwiftUI warning: `Attempting -[UIContextMenuInteraction dismissMenu], when not in an active state`

利用Xcode 12 beta (12A6159) on macOS Catalina 10.15.5 (19F101),在导航电文中,我用“Back”纽顿语,从一种推论中取回,见这一警告。

[UIContextMenuInteraction] Attempting -[UIContextMenuInteraction dismissMenu], when not in an active state. This is a client error most often caused by calling dismiss more than once during a given lifecycle. (<_UIVariableGestureContextMenuInteraction: 0x6000037689a0>)

我没有在Xcode 11.5上收到这一警告。

该法典已经过时:

var body: some View {
    NavigationView {
        NavigationLink(destination: gameScreen) {
            Text("Start game")
        }
    }
}

在浏览到<条码>Screen后,然后在《航海公约》中调用“Back”纽吨,见“灯光”中的gged警告。

我没有升级到新的快速倡议中的任何部分,如<条码>。

问题回答

SwiftUI warning: Attempting -[UIContextMenuInteraction dismissMenu], when not in an active state [Solution]

原始问题: UI 警告:`Attempting - [UIContextMenuInteraction disMenu], 如果没有活跃的国家,“


这一警告信息与使用<代码>有关。 UIContextMenuInteraction in ImmediateUI. 通常的情况是,在你不处于积极状态时,就会出现这种情况。 这种警告往往是在一定寿命期间使用<条码>dismiss()方法不止一次造成的。

In your case, the warning is triggered when you press the "Back" button in the NavigationView. This is because the UIContextMenuInteraction associated with the context menu is still active when you navigate back, and SwiftUI is attempting to dismiss it.

To resolve this warning, you can explicitly handle the dismissal of the context menu when the view disappears. You can achieve this by implementing the onDisappear modifier on the gameScreen view and calling the dismiss() method on the UIContextMenuInteraction:

var body: some View {
    NavigationView {
        NavigationLink(destination: gameScreen) {
            Text("Start game")
        }
    }
}

struct gameScreen: View {
    @State private var contextMenuInteraction: UIContextMenuInteraction?

    var body: some View {
        Text("Game Screen")
            .onAppear {
                self.contextMenuInteraction = UIContextMenuInteraction(delegate: self)
                UIApplication.shared.keyWindow?.addInteraction(self.contextMenuInteraction!)
            }
            .onDisappear {
                self.contextMenuInteraction?.dismiss()
                UIApplication.shared.keyWindow?.removeInteraction(self.contextMenuInteraction!)
            }
    }
}

extension gameScreen: UIContextMenuInteractionDelegate {
    // Implement the necessary delegate methods for the context menu interaction
    // ...
}

By explicitly dismissing the context menu in the onDisappear modifier, you ensure that the dismissal is only called once during the lifecycle of the view, preventing the warning from being triggered.

请注意,该警告是Xcode 12 beta所特有的,可能不出现在Xcode 11.5或以前的版本中。 处理警告和错误,确保你的法典的稳定性和正确性,始终是一种良好做法。





相关问题
SwiftUI: How do I position a button x points above a sheet?

I am trying to recreate a Maps-style UI with a persistent bottom sheet and controls fixed above. Apple Maps UI with controls above ☝️ Example here, the "Look Around" binoculars button and ...

How to access own window within SwiftUI view?

The goal is to have easy access to hosting window at any level of SwiftUI view hierarchy. The purpose might be different - close the window, resign first responder, replace root view or ...

热门标签