Swiftui handling multiple modal dismissal

Hello,

I have to dismiss multiple modal which are presented one after the other from the last modal view.

I have a @state bool property which is being passed as @binding to the concurrent views. When @state property property is set to true .sheet is being fired and modal will be presented.

Could anyone please let me know how to return from Modal2 back to Content view by clicking Dismiss button?

struct ContentView: View {
    @State private var showModal = false
    var body: some View {

       Button("Show Modal") {

          self.showModal = true

       }.sheet(isPresented: $showModal) {

            ModalView(showModal: self.$showModal)

        }
    }
}

struct ModalView: View {
    @Binding var showModal: Bool
    var body: some View {
        VStack {

            Text("Inside Modal View")
                .padding()

            Button("Show Modal") {
               self.showModal = true
            }.sheet(isPresented: $showModal) {
                 Modal2(showModal: self.$showModal)
             }

            Button("Dismiss") {
                self.showModal = false
            }
        }
    }

struct Modal2: View {
    @Binding var showModal: Bool
    var body: some View {
        VStack {

            Text("Inside Modal 2")
                .padding()

            Button("Dismiss") {

                self.showModal.toggle()

            }
        }
    }
}






What is the problem in your shown code?

Sharing the same Binding for two different modal views is not recommended, but with clicking the Dismiss button in Modal2, it returns back to ContentView.
@OOper

The issue with the code above is when Show modal is clicked in Content View the showModal variable is set to true, since it is binded till Modal 2. From Content View, Modal will be presented and then Modal 2 will be presented without being stopped in Modal screen.

Yeah I did not like the approach of sharing Binding variables.

Is there a better approach?
Accepted Answer

Modal 2 will be presented without being stopped in Modal 

Thanks for clarifying. But the behavior is directly caused by sharing the Binding.

Is there a better approach?

Of course. Prepare two distinct sources of truth for two distinct modal views.

An example:
Code Block
import SwiftUI
class ModalState: ObservableObject {
@Published var isModal1Presented: Bool = false
@Published var isModal2Presented: Bool = false
}
struct ContentView: View {
@StateObject var modalState = ModalState()
var body: some View {
Button("Show Modal") {
self.modalState.isModal1Presented = true
}.sheet(isPresented: $modalState.isModal1Presented) {
ModalView(modalState: self.modalState)
}
}
}
struct ModalView: View {
@ObservedObject var modalState: ModalState
var body: some View {
VStack {
Text("Inside Modal View")
.padding()
Button("Show Modal") {
self.modalState.isModal2Presented = true
}.sheet(isPresented: $modalState.isModal2Presented) {
Modal2(modalState: self.modalState)
}
Button("Dismiss") {
self.modalState.isModal1Presented = false
}
}
}
}
struct Modal2: View {
@ObservedObject var modalState: ModalState
var body: some View {
VStack {
Text("Inside Modal 2")
.padding()
Button("Dismiss") {
self.modalState.isModal1Presented = false
self.modalState.isModal2Presented = false
}
}
}
}


@OOPer, thanks for the response. It does work well!

I had one question, what if the modals presented are dynamic? In my scenario I have to display number of modal presentation depending on the response from the server.

Code Block

if the modals presented are dynamic?

Sorry, but I do not understand the context well. You usually declare views with Swift code in SwiftUI, so all views including modals are static.
Can you show a complete example which explains what you think are dynamic?
@OOPer

I'm sorry, an example will be hard to demonstrate since the number of modals are decided each time depending on the response from an API. I will try my best to explain it.

I am setting up the modals one after the other each time depending on the API response. For instance the array elements which decide the number of modals can vary. So, the number of modals will be depending on the number of arrays elements.

In the example you provided it works well if there are few number of modals like three or four.

In my scenario there is minimum seven modal presentations every time.

 I will try my best to explain it.

I really appreciate your effort, but all the description is not enough to make some illustration in my mind.

Hope you will find someone who can help you or can solve your issue yourself. Good luck.
@OOPer

Thanks a.lot for all the help, really appreciate it :)
Swiftui handling multiple modal dismissal
 
 
Q