Contents

AlertScene

A scene that renders itself as a standalone alert dialog.

Declaration

nonisolated struct AlertScene<Actions, Message> where Actions : View, Message : View

Overview

Alert scenes present themselves in the center of the current display, and don’t attach to any particular window. The system prevents interaction with the app until someone dismisses the alert scene.

@main
struct MyApp: App {
    @State var showLoginAlert = true
    @State var loggedIn = false

    var body: some Scene {
        Window("Welcome User Window", id:"WelcomeWindow") {
            ...
        }
        .defaultLaunchBehavior(loggedIn ? .presented : .suppressed)

        AlertScene("Login Required", isPresented: $showLoginAlert) {
            Button("OK") {
                ...
            }
        }
    }
}

All the actions you provide in the ContentBuilder dismiss the alert when someone invokes them. Like the alert modifier, specify the role of the buttons with cancel or destructive. If you don’t provide any actions, the system automatically includes a button with the title “OK” that dismisses the alert scene.

Topics

Initializers

See Also

Presenting an alert