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:
Am I missing something? How can I make sure my state variables are properly read by the sheet view?
1
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")")
}
}
}
}
2
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")")
}
}
}
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)")
}
}
}
4
You do not have to use the state variable in the body, you can just hint to the sheet that it is dependent on the state variable like so:
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){ [showDetails] in <-- add this
VStack {
Text("showDetails: (showDetails ? "yes" : "no")")
}
}
}
}
3