如何使用 SwiftUI 在 MacOS 上显示警报对话框
如何使用 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")
}
}
}
}
How can I display an alert dialog box from a menu item for MacOS apps using SwiftUI
?
The usual code which works for iOS @State var isOn = false
and .alert("title", isPresented: isOn) {..}
doesn't work.
@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")
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常的代码可以正常工作。您永远不会尝试将
Alert
填充到Button
中。你不会在这里这样做。The usual code works fine. You would never try to stuff an
Alert
inside of aButton
. You wouldn't do it here.