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)
}
}
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