I have the following code:
@State private var signoutAlert = false
var body: some View {
Button(action: {
self.signoutAlert = true
print("signout button clicked")
}) {
Text("Sign Out")
}
.alert(isPresented: $signoutAlert) {
print(".alert will display")
//
return Alert(title: Text("Sign Out"), message: Text("Are you sure you want to Sign Out?"), primaryButton: .destructive(Text("Sign Out")) {
print("Signing out....")
self.session.signOut()
self.presentationMode.wrappedValue.dismiss()
}, secondaryButton: .cancel())
}
}
The following output prints out:
- signout button clicked
- .alert will display
I'm expecting the Alert Box to display and prompt the user to either "Cancel" or "Sign Out" by click one of the two buttons; but it is never displayed or prompts the user, which makes no sense!?
Does anyone see anything wrong with my code!? This is extremely frustrating since it should be very simple!?
I had the same problem but realized this is now a deprecated .alert modifier. When I moved to using the newer modifier, it worked fine. Example from Apple's documentation: