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

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()
      }
}

2      

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
            }
        }
    }
}

2      

Thank you very much @ygeras

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!

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.