SwiftUI: Menu

My goal is the selection of different Modal Views over Menu. But on my code, always the first Modal View is selected.

Here is my code:


import SwiftUI



struct ContentView: View {

    @State var showingModal = false

    

    var body: some View {

        NavigationView {

            VStack {

                Text("Content View")

            }

            .navigationBarItems( trailing: actionM )

        }

    }



    var actionM: some View {

        Menu {

            Button("Buy something") { self.showingModal = true }.sheet(isPresented: $showingModal) { ModalBuyView(showModal: self.$showingModal) }

            

            Button("Sell something"){ self.showingModal = true }.sheet(isPresented: $showingModal) { ModalSellView(showModal: self.$showingModal) }

            

        }  label: {

            Image(systemName: "gift.circle")

                .font(.largeTitle)

        }

    }

}



struct ModalBuyView: View {

    @Binding var showModal: Bool

    var body: some View {

        Button("dismiss for BUY ModalView") {

            self.showModal = false

        }

    }

}



struct ModalSellView: View {

    @Binding var showModal: Bool

    var body: some View {

        Button("dismiss for SELL ModalView") {

            self.showModal = false

        }

    }

}





struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}
Answered by BabyJ in 624208022
You should use two different @State properties, one for each modal view.
Because you are using the same variable for both modal views, the first one is presented.
Accepted Answer
You should use two different @State properties, one for each modal view.
Because you are using the same variable for both modal views, the first one is presented.
Thank you BabyJ. For each MenuItem a @State variable solves my issue.
This Solution does not work any more in Beta 5/6 and 7.

I am getting following log output. Any help welcome.
  • [UIContextMenuInteraction updateVisibleMenuWithBlock:] while no context menu is visible. This won't do anything.

Have you solved the problem? It don't work in Xcode v. 12.4.
SwiftUI: Menu
 
 
Q