Is this a Bug ? Alert Not Showing

Hi Team,

Boy I am battling with SwifUI…

I this a bug… My code is pretty simple…

However this code below will not work.

I have another Button below this code for another Alert and it works fine… It looks as if you cannot have to Alerts in the same view?

Help !

    HStack(alignment: .center) {
      Text("Engine:")
        .font(.custom("Arial Rounded MT Bold", size: 30))
        .fontWeight(.bold)
        .foregroundColor(Color.black)
        .padding(.top, -5)
        .padding(.leading, 15)
      
      Button(action:{self.showHelpSheetOne = true}) {
        Image(systemName : "questionmark.circle.fill")
          .resizable()
          .frame(width: 20.0, height: 20.0)
          .accentColor(.black)
      }
      

    Spacer()
    }

    .alert(isPresented: $showHelpSheetOne) {
      Alert(title: Text("Help"), message: Text("Enter the Name of your Two Stroke Engine."), dismissButton: .default(Text("Got it!")))
    }

wait… so you have an alert inside an alert?

Hi,

No I don’t believe so.

Should my code above work?

I have another button with same code but the Alert is called $showHelpSheetTwo and it works fine.

I have no Idea why this button does not work ?

Cheers.

Craig.

@Craig
It looks like you have two ? buttons on your View so I assume that you want to show a different alert depending on which button is pressed. You can have two alerts but you need to set up a switch to display whichever alert you require. As an example, the following is a means I have used to differentiate between multiple alerts on the same page. This uses an enum which defines the ActiveAlert like this:

enum ActiveAlert {
    case errorAuthenticating
    case errorBiometrics
    case showPlaceDetails
}

Define a State variable for the activeAlert and your showingAlert trigger like this:

@State private var activeAlert: ActiveAlert = .showPlaceDetails
@State private var showingAlert = false

In your code you would set the activeAlert accordingly and showingAlert to true to select the alert you wanted to present.

In my case the alerts were defined like so:

.alert(isPresented: $showingAlert) {
    switch activeAlert {
    case .showPlaceDetails:
        return Alert(title: Text(selectedPlace?.title ?? "Unknown"), message: Text(selectedPlace?.subtitle ?? "Missing place information."), primaryButton: .default(Text("OK")), secondaryButton: .default(Text("Edit")) {
            self.showingEditScreen = true
            })

    case .errorAuthenticating:
        return Alert(title: Text("Error"), message: Text("Unable to Authenticate"), dismissButton: .default(Text("OK")))

    case .errorBiometrics:
        return Alert(title: Text("Error"), message: Text("Biometrics not available"), primaryButton: .default(Text("OK")), secondaryButton: .default(Text("Choose Password")) {
            self.choosePassword()
            })
    }
}

I hope some of that makes sense to you.

2 Likes

The other alternative is to define a single alert since you also appear to be just advising what is required in each text field. In the button action set the titleText and the messageText and then set showingAlert = true.

For example:

@State private var titleText = ""
@State private var messageText = ""
@State private var showingAlert = false




Button(action:{
        self.showingAlert = true
        self.titleText = "Help
        self.messageText = "Enter the Name of your Two Stroke Engine."
    }) {
        Image(systemName : "questionmark.circle.fill")
          .resizable()
          .frame(width: 20.0, height: 20.0)
          .accentColor(.black)
      }

} //  End of VStack or HStack 
.alert(isPresented: $showingAlert) {
      Alert(title: Text(self.titleText), message: Text(self.messageText), dismissButton: .default(Text("Got it!")))
}
2 Likes

Hi Chris,

Wow… That’s a great idea… thanks for that… You Rock !!

Thanks again.

Craig.