GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.