0

I have 2 Alerts in 1 view like so (These alerts do work individually), But when combining 2 alerts into the 1 view its only showing alert 2.

I have read that you need to have these attached to different view.

So I have attached the 1 alert to the button and 1 alert to the containing VStack. Still only the second alert is showing. I trying to get both alerts to work.

var body: some View {
     ScrollView {
         VStack (alignment: .leading) {
             ...some stuff
             VStack {
                 Button(action: {
                        dosomestuff
                        showingIntrestedAlert.toggle()
                    }) {
                        Text("Press Me")
                    }.alert(isPresented: $showingIntrestedAlert) {
                        Alert(title: Text("alert1"), message: Text("showing alert 1"), dismissButton: .default(Text("OK")))
                    }
             }
          }
          .alert(isPresented: $fromViewModel.alreadyLikedUser) {
            Alert(title: Text("alert2"), message: Text("alert 2 shown"), dismissButton: .default(Text("OK")))
        }
      }
 }
Gurmukh Singh
  • 1,875
  • 3
  • 24
  • 62

1 Answers1

0

you can insert multiple alert messages in the following way.

First of all: Declare your variables for every single alert you like to handle.

struct xyz : View 
{
    @State private var myFirstAlert : Bool = false
    @State private var mySecondAlert : Bool = false
 ....
}

in your body write your button stuff like this...

var body: some View {
    VStack{
        Text("Hello World")
        Spacer()
        ....
        Button {
            guard someThingIsEmpty else{
                myFirstAlert = true
                return
            }
            guard somethingIsWrong else {
                mySecondAlert = true
                return
            }
            //Button stuff
        } label: { ZStack {Text("Bla"),....}} .alert(isPresented: self.$myFirstAlert){
         Alert(title: Text("Something went wrong!"))}
    .alert(isPresented: self.$mySecondAlert){
    Alert(title: Text("That was shitty"), message: Text("Something went totally wrong"), dismissButton: .default(Text("Urghs"), action: {}}
Iskandir
  • 937
  • 1
  • 9
  • 21