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?