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 String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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.