BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: How to quit .fullScreenCover ?

Forums > 100 Days of SwiftUI

So here is the situation. I have screen A,B and C. Screen A I have the code to open screen B as full screen

@State private var isPresented = false

    var body: some View {
        Button("Present!") {
            isPresented = true
        }
        .fullScreenCover(isPresented: $isPresented, content: B.init)
    }

Now I am in screen B where I can quit the screen and go back to screen A Or I have another button that takes me to screen C with the same code as above. Screen C has also option to quit and to open screen B again as full screen. After playing around a bit I noticed if you open screen B and C from each other a few times then when you click "quit" it wont exit both screens and take you to screen A but it will only go to the previous screen.

How can I program the button "quit" to exit both screen B and C and go back to A. I want this to happen no matter how many times I have opened screens B and C. Also for the button quit in B and C I used the

@Environment(\.dismiss) var dismiss

var body: some View {
      Button("Quit") {
            dismiss()
      }
}

   

Hi! I think the quickest way is to pass your initial isPresented as a binding to fullScreenCovers and if you created too many stacks and just want to jump back to initial screen you can change it false.

something like this:

struct ViewA: View {
    @State private var isRootPresented = false

    var body: some View {
        VStack(spacing: 20) {
            Text("This is A view")
            Button("Present B View") {
                isRootPresented = true
            }
            .fullScreenCover(isPresented: $isRootPresented) {
                ViewB(root: $isRootPresented)
            }
        }
    }
}

struct ViewB: View {
    @Environment(\.dismiss) var dismiss
    @State private var isPresented = false
    @Binding var root: Bool

    var body: some View {
        VStack(spacing: 20) {
            Text("This is B view")

            Button("Present C View") {
                isPresented = true
            }
            .fullScreenCover(isPresented: $isPresented) {
                ViewC(root: $root)
            }

            Button("Dismiss") {
                dismiss()
            }

            Button("Back to root") {
                root = false
            }
        }
    }
}

struct ViewC: View {
    @Environment(\.dismiss) var dismiss
    @State private var isPresented = false
    @Binding var root: Bool

    var body: some View {
        VStack(spacing: 20) {
            Text("This is C view")

            Button("Present B View") {
                isPresented = true
            }
            .fullScreenCover(isPresented: $isPresented) {
                ViewB(root: $root)
            }

            Button("Dismiss") {
                dismiss()
            }

            Button("Back to root") {
                root = false
            }
        }
    }
}

   

Thank you very much @ygeras

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

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.