Here's what I think....
In your Settings
view, you were creating a second UserSettings object. So when the application starts, it pulls the value from the user settings file. Just as you stated. But this second object never changes, so it never updates its view. What's confusing is you gave it the same name as the other UserSettings object.
So instead of creating a new, separate object (with the same name!) you should pass in the userSettings object created in your ContentView
. Because it is an observed object, whenever it's changed elsewhere, the Settings
view struct will redraw itself.
struct Settings: View {
@ObservedObject var userSettings: UserSettings // Pass it in. Don't declare a new one.
var body: some View {
NavigationView{
// ...... snip .............
Of course, this requires a small change to your ContentView code, right?
struct ContentView: View {
@StateObject var userSettings = UserSettings() // Yes! Declare one class object
var body: some View {
TabView{
Settings(userSettings: userSettings) // pass it in!
.tabItem{
Label("Settings", systemImage: "person")
}
// ....... snip ..........
PS: Nice updating your score variable. Now consider:
- Rename Settings to
SettingsView
. Why? It's a view! Not a collection of settings.
- Rename
ContentView
. Why? Just because.