I have an array of objects presented via a list in a parent view. Each row has a navigation link to a child view that is passed a binding to the object. The child view allows basic editing of the selected item. The child view also contains a button that presents a sheet to a default list of objects the user can "swap" the selected object to.
Swapping the object works, however the child view is only updated after navigating all the way back to the parent.
Am I missing something that is not allowing the child view to update after dismissing the "swap list"?
I do not have this issue when the data is not presented via a list.
Here are some trimmed down examples... The swap list is replaced with a button in the TestEditorView.
Data presented via array:
** child view does not update until navigating back to parent **
struct TestChildBindingList: View {
@State private var text = ["This is some mutable text", "Please mutate", "Hopefully mutable text"]
var body: some View {
NavigationStack {
VStack {
List {
ForEach($text, id: \.self) { $item in
NavigationLink(item) {
TestChildView(text: $item)
}
}
}
}
.navigationTitle("Parent View")
}
}
}
struct TestChildView: View {
@Binding var text: String
@State private var showingChangeText = false
var body: some View {
VStack {
Text(text)
Button("Change the text") {
showingChangeText = true
}
.sheet(isPresented: $showingChangeText) {
TestEditorView(text: $text)
}
}
.navigationTitle("Child View")
}
}
struct TestEditorView: View {
@Environment(\.dismiss) var dismiss
var text: Binding<String>?
var body: some View {
Button("Press here") {
text?.wrappedValue = "I pressed the button"
dismiss()
}
}
}
struct TestChildBindingList_Previews: PreviewProvider {
static var previews: some View {
TestChildBindingList()
}
}
Data presented via single object:
** Child view updates correctly without navigating back to parent **
struct TestChildBindingItem: View {
@State private var text = "This is definately mutable"
@State private var showingChild = false
var body: some View {
NavigationStack {
VStack {
Text(text)
Button("Show Child View") {
showingChild = true
}
}
.sheet(isPresented: $showingChild) {
TestChildItemView(text: $text)
}
.navigationTitle("Parent View")
}
}
}
struct TestChildItemView: View {
@Binding var text: String
@State private var showingChangeText = false
var body: some View {
VStack {
Text(text)
Button("Change the text") {
showingChangeText = true
}
.sheet(isPresented: $showingChangeText) {
TestEditorView(text: $text)
}
}
.navigationTitle("Child View")
}
}
struct TestEditorItemView: View {
@Environment(\.dismiss) var dismiss
var text: Binding<String>?
var body: some View {
Button("Press here") {
text?.wrappedValue = "I pressed the button"
dismiss()
}
}
}
struct TestChildBindingItem_Previews: PreviewProvider {
static var previews: some View {
TestChildBindingItem()
}
}
Thank you for any help offered!