1

I found this tutorial to pop to a RootView through a series of NavigationLinks.

I would like to do the same thing but pop to the RootView in a SheetView.

This is what the structure of my app is like:

struct RootController: View {
   @State var isPresented = true

   var body: Some View {
       NavigationView {
          FirstView()
       }
       .environment(\.rootPresentationMode, self.$isPresented)
   }
}

struct FirstView: View {
    @Environment(\.rootPresentationMode) private var rootPresentationMode: Binding<RootPresentationMode>

   var body: Some View {
        NavigationLink(destination: SecondView,
           isActive: self.$pushNewProject) {
             EmptyView()
        }
        .isDetailLink(false)
        .hidden()
   }
}

struct SecondView: View {
    @Environment(\.rootPresentationMode) private var rootPresentationMode: Binding<RootPresentationMode>
    @State private var isShowingSheetView:Bool = false

   var body: Some View {
          Text(...)
          .sheet(isPresented: $isShowingEditProjectSheet){
           SheetView(showingSheetView: self.$isShowingSheetView)
        }
   }
}

And my SheetView (where I want to trigger the pop back to the RootView):

struct SheetView: View {
    @Environment(\.rootPresentationMode) private var rootPresentationMode

    var body: Some View {
        Text()
        .alert("Are you sure you want to delete?", isPresented: $showingDeleteAlert){
                Button("Cancel", role: .cancel){
                }
                Button("Delete"){
                    print("attempt to pop")
                    self.rootPresentationMode.wrappedValue.dismiss()
                }
            }
     }
}

after adding this extension

struct RootPresentationModeKey: EnvironmentKey {
    static let defaultValue: Binding<RootPresentationMode> = .constant(RootPresentationMode())
}

extension EnvironmentValues {
    var rootPresentationMode: Binding<RootPresentationMode> {
        get { return self[RootPresentationModeKey.self] }
        set { self[RootPresentationModeKey.self] = newValue }
    }
}

typealias RootPresentationMode = Bool

extension RootPresentationMode {
    
    public mutating func dismiss() {
        self.toggle()
    }
}

Any ideas how to pop to root from a SheetView?

scientiffic
  • 9,045
  • 18
  • 76
  • 149
  • Can you create a [mre]? This doesn't compile. – George Sep 10 '21 at 21:34
  • you are over complicating your code, a simple var passing using binding would do. If you still want to use your complicated setup, you need to specifically pass the environment to a sheet. Something like: `SheetView(showingSheetView: self.$isShowingSheetView).environment(rootPresentationMode) ` In `SheetView` you also need to add `@Binding var isShowingSheetView: Bool` – workingdog support Ukraine Sep 10 '21 at 23:23
  • @workingdog even if I passed a binding var, how do I specify that I want to go back to the root from a nested child view? – scientiffic Sep 14 '21 at 16:43

0 Answers0