NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Cascading Sheets

Forums > SwiftUI

Curious if anyone can explain to me why, after tapping the words "Sheet 4," the screen returns to "Sheet 1" rather than "Foundation":

struct ContentView: View {
    @State private var showSheet1: Bool = false
    @State private var showSheet2: Bool = false
    @State private var showSheet3: Bool = false
    @State private var showSheet4: Bool = false

    var body: some View {
        Button(action: {
            showSheet1.toggle()
        }) {
            Text("Foundation")                          // Click this phrase so that Sheet1=T, Sheet2=F, Sheet3=F, Sheet4=F
        }
        .sheet(isPresented: $showSheet1) {              // Because Sheet1 is T...

            Button(action: {
                showSheet2.toggle()
            }) {
                Text("Sheet 1")                         // ...this appears. Click this phrase so that Sheet1=T, Sheet2=T, Sheet3=F, Sheet4=F
            }
            .sheet(isPresented: $showSheet2) {          // Because Sheet2 is T...
                Button(action: {
                    showSheet3.toggle()
                }) {
                    Text("Sheet 2")                     // ...this appears. Click this phrase so that Sheet1=T, Sheet2=T, Sheet3=T, Sheet4=F
                }
                .sheet(isPresented: $showSheet3) {      // Because Sheet3 is T...
                    Button(action: {
                        showSheet4.toggle()
                    }) {
                        Text("Sheet 3")                 // ...this appears. Click this phrase so that Sheet1=T, Sheet2=T, Sheet3=T, Sheet4=T
                    }
                    .sheet(isPresented: $showSheet4) {  // Because Sheet4 is T...
                        Button(action: {
                            showSheet4.toggle()
                            showSheet3.toggle()
                            showSheet2.toggle()
                            showSheet1.toggle()
                        }) {
                            Text("Sheet 4")             // ...this appears. Click this phrase so that Sheet1=F, Sheet2=F, Sheet3=F, Sheet4=F
                        }
                    }
                }
            }
        }
    }
}

As the View is reloaded each time the text is tapped, doesn't Swift start over from the var body: some View each time, examine the values of the four variables, and then proceed through the code until it reaches a false .isPresented. But something weird's going on with the first sheet.

Also weird is the fact that if you tap "Sheet 4" a second time through (i.e., Foundation -> 1 -> 2 -> 3 -> 4 -> 1 -> 2 -> 3 ->4) the Console prints out a bunch of goobledegook and "Sheet 2" appears, and then it's a continuous loop of 2 -> 3 -> 4 -> 2 -> .... Which makes even less sense to me than restarting at 1 the first time around.

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.