I'd like to update an UI element on an overview view when data on another view is changed.
I looked into @EnvironmentalObject and @Binding. However, an update to either object does not appear to force a view reload. Only changes to @State force renders.
Also, in the case described below, the ChangeView is not a child of OverviewView. Therefore @Binding is not an option anyway.
Data.swift
struct ExampleData : Hashable { var id: UUID var name: String } var globalVar: ExampleData = ExampleData(id: UUID(), name:"") OverviewView.swift
struct OverviewView: View { @State private var data: ExampleData = globalVar var body: some View { Text(data.name) } } ChangeView.swift
struct ChangeView: View { @State private var data: ExampleData = globalVar var body: some View { TextField("Name", text: $data.name, onEditingChanged: { _ in globalVar = data }, onCommit: { globalVar = data }) } } Changes within the ChangeView TextField will update the globalVar. However, this will not update the Text on the OverviewView when switching back to the view.
I am aware that using global variables is "ugly" coding. How do I handle data that will be used in a multitude of unrelated views?
Please advise on how to better handle such a situation.