Can I display an Alert from a context menu?

I am currently trying to display an alert to the user when they click on an item from the context menu. It is currently not alerting. Below is my code and please ignore formatting since I did copy and paste.

.toolbar {
     ToolbarItem(placement: .navigationBarTrailing) {
          Menu(content: {
               Button("Disconnect") {
                    showAlert = true
               }
               .alert(isPresented: $showAlert) {
                     Alert(
                          title: Text("Disconnect"),
                          message: Text("Test."),
                          primaryButton: .default(
                               Text("Okay")
                          ),
                               secondaryButton: 
                               .destructive(Text("Cancel")
                               )
                           )
                    }
                    Button("Reboot") {
                        // empty for now
                   }
                    Button("Factory Reset") {
                        // empty for now
                    }
                }, label: {Image(systemName: "ellipsis")})
                    .foregroundColor(.black)
                    .imageScale(.large)
            }
        }
Answered by Claude31 in 690998022

You need to call the alert outside of the toolbar.

This example should help:

https://stackoverflow.com/questions/65433858/swiftui-show-alert-from-button-inside-of-toolbaritem

Accepted Answer

You need to call the alert outside of the toolbar.

This example should help:

https://stackoverflow.com/questions/65433858/swiftui-show-alert-from-button-inside-of-toolbaritem

Can I display an Alert from a context menu?
 
 
Q