【问题标题】:How do I send an alert in front of a popover?如何在弹出框前发送警报?
【发布时间】:2021-01-10 12:34:26
【问题描述】:

我正在编写一个菜单栏应用程序,其中只有一个弹出框,即操作所在的位置。目标是 MacOS 10.15 Catalina。

在弹出窗口中,我想显示一个警报。代码是这样的:

struct ContentView: View {
    @State private var confirm = false
    var body: some View {
        return VStack {
            Button("Reset Settings", action: {
                self.confirm = true
            })
            .alert(isPresented: $confirm) {
                Alert(
                    title: Text("Do you really want to?"),
                    message: Text("Do you want to talk about it?"),
                    primaryButton: .default(Text("Oh, yeah")) {
                        print("Well, if you insist …")
                    },
                    secondaryButton: .cancel()
                )
            }
        }
    }
}

警报运行良好,但它出现在 弹出框之后。

如何将警报放在弹出框前面?

【问题讨论】:

    标签: swift macos swiftui alert popover


    【解决方案1】:

    我假设警报是在弹出框将显示在顶部的视图上声明的(我在您的代码中看不到弹出框,所以我不能确定)。如果是这种情况,那么您需要在弹出框本身中声明您的警报,而不是在其背后的视图中。

    示例:

    var body: some View {
        VStack {
            Button("Reset Settings", action: {
                    self.confirm = true
                })
                .popover(isPresented: $popoverPresentation) {
                    
                    PopoverView()
                        .alert() // declare alert here, inside the popover,
                    // or even inside the `PopoverView`
                    
                }
                // .alert() dont declare your alert here, outside the popover
        }
    }
    

    【讨论】:

    • 对不起,我不遵守。视图确实在视图中声明,但所有教程都是这样做的。我如何在弹出窗口中声明视图,而不是在视图中?
    • 更新了一个更好的例子。同样的问题也发生在我身上,但我仍然不能 100% 确定您的问题与我的相同(尽管我认为是这样)。
    • 我从未听说过 PopoverView。 XCode 也没有,因为我收到了 Cannot find 'PopoverView' in scope 的消息。
    • O,PopoverView 只是一个示例视图。通过 PopoverView,我的意思是您考虑放入弹出框的任何内容。它可能是一个文本,可能是一个包含某些内容的 VStack,可能只是一个按钮,或者任何你想要的东西,这取决于你。
    • 谢谢。我用Text 尝试了你的建议,但警报仍然出现在弹出框后面。
    猜你喜欢
    • 1970-01-01
    • 2012-08-28
    • 2017-03-26
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多