Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI sheet doesn't access the latest value of state variables on first appearance

Tags:

ios

swift

swiftui

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?

like image 315
marcgg Avatar asked Nov 18 '25 20:11

marcgg


2 Answers

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")")
            }
        }
    }
}
like image 97
muvaaa Avatar answered Nov 21 '25 10:11

muvaaa


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")")
        }
    }
}
like image 28
Asperi Avatar answered Nov 21 '25 09:11

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!