簡體   English   中英

如何使用 SwiftUI 在 MacOS 上顯示警報對話框

[英]How to display Alert dialog on MacOS using SwiftUI

如何使用SwiftUI從 MacOS 應用程序的菜單項中顯示警告對話框? 適用於 iOS @State var isOn = false.alert("title", isPresented: isOn) {..}的常用代碼不起作用。

@main
struct MyApp: App {

var body: some Scene {
    WindowGroup {
        ContentView()
    }.commands {
        CommandMenu("Test menu") {
            Button(action: {
                // I want to show an alert dialog dialog here.
            }) {
                Text("Click Me")
            }
        }
    }
}

通常的代碼工作正常。 您永遠不會嘗試在Button中填充Alert 你不會在這里做的。

@main
struct MyApp: App {

    @State var isOn = false
    
    var body: some Scene {
        WindowGroup {
            NavigationView {
                ContentView()
                    .alert("title", isPresented: $isOn) {
                        Button("OK", role: .cancel) { }
                    }
            }
        }.commands {
            CommandMenu("Test menu") {
                Button(action: {
                    isOn = true
                }) {
                    Text("Click Me")
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM