问题
When I run the following code compiled for iOS14 on iOS14 sim or device, the updated @State variable "selection" doesn’t get passed to a view displayed as .sheet(… The exact code runs OK on iOS 13.5 sim or 13.6 device
The code runs as expected on iOS 14, if I place somewhere in the view a Text(“”) and display the variable being updated.
It seems that only if I display the value, the .sheet get's rendered again updating the MyView(tenum: selection) with the correct value.
In the code below just uncomment the line below to make it work in iOS14
// Text("Currently: (selection.rawValue)")
import SwiftUI
struct ContentView:View {
@State private var selection=MyView.TestEnum.ONE
@State private var presentSheet=false
var body: some View {
VStack {
// Text("Currently: \(selection.rawValue)")
Button(action: {
selection=MyView.TestEnum.TWO
print("selecting: \(selection.rawValue)")
presentSheet=true
}, label: {
Text("Select TWO")
})
.sheet(isPresented: $presentSheet, onDismiss: {
print("dismissed")
}) {
MyView(tenum: selection)
}
}
}
}
struct MyView:View {
enum TestEnum: String, Codable {
case ONE="ONE"
case TWO="TWO"
}
var tenum:TestEnum
var body: some View {
Text("enum: \(tenum.rawValue)")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
来源:https://stackoverflow.com/questions/63952133/is-this-intended-state-var-behaviour-in-ios14-or-is-it-a-bug