alert(isPresented:content:)
Presents an alert to the user.
Declaration
nonisolated func alert(isPresented: Binding<Bool>, content: () -> Alert) -> some View
Parameters
- isPresented:
A binding to a Boolean value that determines whether to present the alert that you create in the modifier’s
contentclosure. When the user presses or taps OK the system setsisPresentedtofalsewhich dismisses the alert. - content:
A closure returning the alert to present.
Discussion
Use this method when you need to show an alert to the user. The example below displays an alert that is shown when the user toggles a Boolean value that controls the presentation of the alert:
struct OrderCompleteAlert: View {
@State private var isPresented = false
var body: some View {
Button("Show Alert", action: {
isPresented = true
})
.alert(isPresented: $isPresented) {
Alert(title: Text("Order Complete"),
message: Text("Thank you for shopping with us."),
dismissButton: .default(Text("OK")))
}
}
}[Image]