11

It seems like the state variable are not properly updated when a sheet is displayed for the first time.

For instance with this code:

import SwiftUI

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            VStack {
                Text("showDetails: \(showDetails ? "yes" : "no")")
            }
        }
    }
}

struct DemoView_Previews: PreviewProvider {
    static var previews: some View {
        DemoView()
    }
}

This will display "no" on first click, and "yes" on second, as showcased here:

enter image description here

Am I missing something? How can I make sure my state variables are properly read by the sheet view?

marcgg
  • 65,020
  • 52
  • 178
  • 231

3 Answers3

10

You see value on time of sheet creation. If you want to track parent view state create sheet subview with binding to that state, like below. Binding will update subview when subview will appear.

Tested with Xcode 12 / iOS 14
Re-tested with Xcode 13.3 / iOS 15.4

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            SheetDetailView(flag: $showDetails)
        }
    }
}

struct SheetDetailView: View {
    @Binding var flag: Bool
    var body: some View {
        VStack {
            Text("showDetails: \(flag ? "yes" : "no")")
        }
    }
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
3

While there are answers here that show how to mitigate this issue, I feel none of them explains why the issue happens or why the fix works which is in my opinion more important then blindly using a fix.

Basically, it seems that SwiftUI will reevaluate the DemoView body if any @State property is accessed when it first evaluates it. Since showDetails property is set only in the Button action callback and accessed in the sheet content view builder callback which are not executed in the body SwiftUI doesn't appear to know that it needs to reevaluate the body when it's updated on tap.

You can check that by adding a simple print in the body var like so:

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        print("Body evaluated")
        return VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            VStack {
                Text("showDetails: \(showDetails ? "yes" : "no")")
            }
        }
    }
}

After tapping the button there will be no body reevaluation. This is why showDetails value captured in the sheet is the initial false value.

If you were to use showDetails variable in the DemoView body, either displaying it, or just adding a simple print, you will see that the body is reevaluated and value you see in the sheet is up to date.

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        print("Body evaluated \(showDetails)")
        return VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            VStack {
                Text("showDetails: \(showDetails ? "yes" : "no")")
            }
        }
    }
}
muvaaa
  • 450
  • 4
  • 12
  • 1
    I'd like to add to this as I think it's the best answer. I had a simalar problem (didn't realise this was the same until this answer) and malhal answered it on mine (here: https://stackoverflow.com/a/76170215/769294) saying you can just add `[showDetails] in` at the start of the sheet closure e.g. `.sheet(isPresented: $showDetails) { [showDetails] in` and basically my understanding is it makes the view re-evaluate `showDetails` before passing it to the closure as it now knows it needs it! Feels much tidier if you don't need two way binding and just want the value passed in to the child view. – GazB May 11 '23 at 16:33
0

Sometimes it's not possible to use @Binding. For example if we need to use a variable in the init block. My way:

struct DemoView: View {

    @State var showDetails = false
    @State var wtfGuys = false

    var body: some View {
        VStack {
            Button(action: {
                showDetails = true
            }) {
                Text("Show sheet")
            }
        }
                .sheet(isPresented: $showDetails) {
                    ZStack {
                        if wtfGuys {
                            VStack {
                                Text("showDetails: \(showDetails ? "yes" : "no")")
                            }
                        }
                    }
                            .onAppear { wtfGuys = true }
                            .onDisappear { wtfGuys = false }
                }
    }
}

Example with init block:

struct DemoView: View {

    @State var showDetails = false
    @State var wtfGuys = false

    var body: some View {
        VStack {
            Button(action: {
                showDetails = true
            }) {
                Text("Show sheet")
            }
        }
                .sheet(isPresented: $showDetails) {
                    ZStack {
                        if wtfGuys {
                            DetailView(showDetails: showDetails)
                        }
                    }
                            .onAppear { wtfGuys = true }
                            .onDisappear { wtfGuys = false }
                }
    }
}

struct DetailView: View {

    let showDetails: Bool

    init(showDetails: Bool) {
        print(showDetails) // true
        self.showDetails = showDetails
    }

    var body: some View {
        Text("showDetails: \(showDetails ? "yes" : "no")")
    }
}

One more case:

struct DemoView: View {

    @State var showDetails = false
    @State var detailsText = ""

    var body: some View {
        VStack {
            Button(action: {
                detailsText = "new text"
                showDetails = true
            }) {
                Text("Show sheet")
            }
        }
                // Here it is
                .onChange(of: showDetails) { _ in }
                .sheet(isPresented: $showDetails) {
                    Text("text: \(detailsText)")
                }
    }
}
Ivan Medvedev
  • 63
  • 1
  • 4
  • This is what i am actually looking for because i do not want to create another struct and binding just for this. However, in the animation, i noticed it shows weirdly. Does your animation run smooth when showing the sheet? – chitgoks Nov 10 '22 at 10:55
  • @chitgoks For these examples the animation is smooth. I've experienced bad animation when I put onAppear/onDisappear inside "if wtfGuys". – Ivan Medvedev Nov 12 '22 at 11:53
  • I got it to work. Though I have a different problem now, if i display it as html, i get the AttributeGraph: cycle detected through attribute In Sheet. it's a different issue. thanks for this. – chitgoks Nov 12 '22 at 13:03
  • @chitgoks I added one more case to the answer. – Ivan Medvedev Dec 14 '22 at 11:13