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

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

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.