BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

Dismissing multiple view

Forums > SwiftUI

Hi guys, how can I dismiss a sheet containg a navigationLink which contains another navigationLink, please? When I dismiss it brings me back to the preview view but I want to return for instance directly in ContentView.

Thanks in advance for your help

   

One of the ways is to pass @Binding that triggers the sheet. Or you can pass it to the environment and use it from there. Depending on the architechture of your project there are other possibilities to implement the same.

struct ContentView: View {
    @State private var showNavLinkOne = false

    var body: some View {
        VStack(spacing: 10) {
            Text("This is Content View")
            Button("Show NavLink1") {
                showNavLinkOne.toggle()
            }
        }
        .padding()
        .sheet(isPresented: $showNavLinkOne) {
            NavLinkOneView(goToContentView: $showNavLinkOne)
        }
    }
}

struct NavLinkOneView: View {
    @Binding var goToContentView: Bool

    var body: some View {
        NavigationStack {
            VStack {
                Text("This is Navigation One View")
                NavigationLink("Show NavLink2") {
                    NavLinkTwoView(goToContentView: $goToContentView)
                }
            }

        }
    }
}

struct NavLinkTwoView: View {
    @Binding var goToContentView: Bool

    var body: some View {
        NavigationStack {
            Text("This is Navigaiton Two View")
            Button("Go to ContentView") {
                goToContentView.toggle()
            }
        }
    }
}

You can have a look at this thread as well.

https://www.hackingwithswift.com/forums/swiftui/is-it-possible-to-close-modal-view-from-detail-link/19755/19760

   

Thanks a lot @ygeras, that worked.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.