Action Sheet in SwiftUI is a great tool to show user show few possible actions.
This example will have two buttons, and each of them will increment or decrement some value.
First, create two state property, one to keep some number, and the second to keep if the action sheet is on the screen.
@State private var counter = 0
@State private var showSheet = false
Next, create VStack with Text to display that counter and Button to invoke Action Sheet.
VStack {
Text(“Counter: \(counter)“)
.padding()
Button(“Show sheet”) {
showSheet = true
}
}
The last step is to use .actionSheet view modifier to show Action Sheet with two buttons.
.actionSheet(isPresented: $showSheet) {
ActionSheet(title: Text(“Action sheet”),
message: Text(“Message”),
buttons: [
.default(Text(“+”), action: {
counter += 1
}),
.cancel(Text(“-“), action: {
counter -= 1
})
])
}