UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.