TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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

2      

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

2      

Thanks a lot @ygeras, that worked.

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.