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

SOLVED: Making a parent view to dismiss child one

Forums > SwiftUI

On my app quite complex hiearchy I need to dismiss a child view with @Environment dismiss() from its parent view (just the main "schema" is displayed here):

struct ChildView: View {
@Binding var showingDetail: Bool
@Environment(\.dismiss) var dismiss

     var body: some View {
     //...
   }
 }

struct ParentView: View {
@State private var showingDetail: Bool

    var body: some View {
        ChildView()
        //...

        .onChange(of: ... ){ _ in
      showingDetail = false
    }

I'm trying with @Bindings using that showingDetail property, without success. Does anybody knows a way to deal with it?

Thanks.

2      

Not sure, how complex your hiearchy is, however the logic below works just fine. So passing the binding to child view will dismiss it. NavigationStack will work a bit different way, but can be controlled via NavigationPath.

struct ParentView: View {
    @State private var showChildView = false

    var body: some View {
        VStack {
            Text("This is parent view")
                .padding(.bottom)

            Button("Show Child View") {
                showChildView = true
            }
            .sheet(isPresented: $showChildView) {
                ChildView(showChildView: $showChildView)
            }
        }
    }
}

struct ChildView: View {
    @Binding var showChildView: Bool

    var body: some View {
        VStack {
            Text("This is child view")
                .padding(.bottom)

            Button("Dismiss") {
                showChildView = false
            }
        }
    }
}

2      

Thank you, @ygeras. My true intention is to make it change automatically, with no button tap. I've finally solved with a maybe-not-so-ortodox but functional workaround: using an @AppStorage with a conditional value, and not dismissing the view but:

struct ChildView: View {
@AppStorage("showingDetailView") var showingDetailView = true

     var body: some View {
      if !showingDetailView {
          NoDataView()
          } else {
        MyView()
        //...
        }
   }
 }

struct ParentView: View {
@AppStorage("showingDetailView") var showingDetailView = true
@State selection: Int

    var body: some View {
        ChildView()
        //...
        }

        .onChange(of: selection ){ _ in
      showingDetail = false
    }

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.