Group Group Group Group Group Group Group Group Group

presentationMode dismiss issue

Hi,

I am working through the iOS developer path and I am on the “your second iOS and SwiftUI app” course.
In the “Adding and deleting tasks”, we dismiss the modal using

self.presentationMode.wrappedValue.dismiss()

However, when the modal dismisses, clicking on the “plus” button is not bringing the modal back up. When I added a breakpoint in the “action” method, that breakpoint is not getting hit. Removing out the code to dismiss the modal and just dismissing the modal by swiping down allows me to bring up that modal again.

Is anyone else facing the same issue? I tried googling to see if it is a common issue but couldn’t find it.
Any help with this would be greatly appreciated!

@roguescroll Do you still have issues with this?

Hi @roguescroll,

The modal is dismissed automatically, the modal is displayed based on a boolean value, and when you dismiss it is automatically toggled. You don’t have to manually dismiss it again from code.

cheers,

You can use presentationMode environment variable in your modal view and calling self.presentaionMode.wrappedValue.dismiss() to dismiss the modal:

struct ContentView: View {

@State private var showModal = false

var body: some View {
Button(action: {
self.showModal = true
}) {
Text(“Show modal”)
}.sheet(isPresented: self.$showModal) {
ModalView()
}
}
}

struct ModalView: View {

@Environment(.presentationMode) var presentationMode: Binding

var body: some View {
Group {
Text(“Modal view”)
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text(“Dismiss”)
}
}
}
}