To display an alert in SwiftUI, you can use the alert
modifier along with the Alert
struct. Here's an example of how to create and display an alert in SwiftUI:
main.swift389 chars15 lines
In the example above, we have a button that sets the showingAlert
state variable to true when tapped. This triggers the alert
modifier, which presents the alert when showingAlert
is true.
The Alert
struct creates an alert with the specified title, message, and dismiss button. You can customize the alert further by adding more buttons or actions.
Note that alert
is a modifier that needs to be attached to a view, which is why we wrap the button and the alert modifier in a VStack
in this example.
Hope this helps!
gistlibby LogSnag