How can I apply a check mark to a single Button() menu item in SwiftUI?

Viewed 49

I am trying to implement a single menu item that achieves three things when it is selected:

  • Toggle a Boolean value true/false.
  • Perform an action based on the new value.
  • Show a checkmark next to the menu item when the new value is true.

I can't figure out how to add the checkmark. I tried using a Toggle() instead of a Button() but I could not get it to behave correctly. I couldn't get it to perform the action at the time the menu item is selected.

The following code behaves the way I want to but does not show a checkmark.

struct MenuView: View {
    @Binding var checkMarkOn: Bool
    
    var body: some View {
        
        Menu("Toggle Buttons") {
            Button("Showing Checkmark") {
                checkMarkOn = !checkMarkOn
                performAction(with: checkMarkOn)
            }
        }
    }
}
0 Answers
Related