On iOS 13 I used to use optional @State properties to adapt views. In my case, the presented view would either create a new object (an assignment) if the state that is passed into it is nil, or edit the assignment if an assignment was passed in. This would be done in the action block of a Button and it worked beautifully.
On iOS 14 / Xcode 12 this no longer seems to work. Given the following code which creates a new assignment and passes it into the editor view when the user taps a "New Assignment" button, the value of assignment remains nil. Is anyone else experiencing similar behaviour?
struct ContentView: View {
@Environment(\.managedObjectContext) var context
@State var assignmentEditorIsPresented = false
@State var assignment: Assignment? = nil
var Body: some View {
[...]
Button("New Assignment", action: {
self.assignment = Assignment(context: context)
self.assignmentEditorIsPresented = true
})
.sheet(isPresented: assignmentEditorIsPresented) {
[...]
}
}
}
I'm at a complete loss. This has been an issue for me since beta 1 and is present up to the current beta 3. As a workaround, I've been displaying the description of the object in a hidden Text view which fixes the problem:
Text("\(assignment?.description ?? "")")
.hidden()
It seems to work maybe 50% of the time but is less than ideal. I just hope this is a bug and not a design change. Am I doing something wrong? Has anyone else had a similar issue?