3

In the following example, I have a view that shows a sheet of ViewOne. ViewOne has a NavigationLink to ViewTwo.

How can I dismiss the sheet from ViewTwo?

Using presentationMode.wrappedValue.dismiss() navigates back to ViewOne.

struct ContentView: View {

    @State private var isShowingSheet = false

    var body: some View {
        Button("Show sheet", action: {
            isShowingSheet.toggle()
        })
        .sheet(isPresented: $isShowingSheet, content: {
            ViewOne()
        })
    }
}

struct ViewOne: View {

    var body: some View {
        NavigationView {
            NavigationLink("Go to ViewTwo", destination: ViewTwo())
                .isDetailLink(false)
        }
    }
}

struct ViewTwo: View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        Button("Dismiss sheet here") {
            presentationMode.wrappedValue.dismiss()
        }
    }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Isaak
  • 1,107
  • 2
  • 11
  • 29
  • Does this work for you? https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view – nickreps Jul 17 '21 at 23:26

1 Answers1

4

This may depend some on platform -- in a NavigationView on macOS, for example, your existing code works.

Explicitly passing a Binding to the original sheet state should work:

struct ContentView: View {

    @State private var isShowingSheet = false

    var body: some View {
        Button("Show sheet", action: {
            isShowingSheet.toggle()
        })
        .sheet(isPresented: $isShowingSheet, content: {
            ViewOne(showParentSheet: $isShowingSheet)
        })
    }
}

struct ViewOne: View {
    @Binding var showParentSheet : Bool
    
    var body: some View {
        NavigationView {
            NavigationLink("Go to ViewTwo", destination: ViewTwo(showParentSheet: $showParentSheet))
                //.isDetailLink(false)
        }
    }
}

struct ViewTwo: View {
    @Binding var showParentSheet : Bool
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        Button("Dismiss sheet here") {
            showParentSheet = false
        }
    }
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94