Post

Replies

Boosts

Views

Activity

Reply to How to save settings selections
import SwiftUI struct CardTheme: View { //@State private var theme = 0 @State private var theme = UserDefaults.standard.integer(forKey: "Card Theme") var body: some View { List { HStack { Text("Mono") //.font(.system(size: 12)) .onTapGesture { self.setTheme(i: 0) } Spacer() if(theme == 0) { Image(systemName: "checkmark") .foregroundColor(Color.green) } } HStack { Text("Cool") // .font(.system(size: 12)) .onTapGesture { self.setTheme(i: 1) } Spacer() if(theme == 1) { Image(systemName: "checkmark") .foregroundColor(Color.green) } } HStack { Text("Cute") // .font(.system(size: 12)) .onTapGesture { self.setTheme(i: 2) } Spacer() if(theme == 2) { Image(systemName: "checkmark") .foregroundColor(Color.green) } } } .navigationBarTitle(Text(verbatim: "Card Theme")) } func setTheme(i: Int) { theme = i UserDefaults.standard.set(i, forKey: "Card Theme") } }Sorry I'm having trouble following your code. But if I modified mine to use UserDefaults, i can see that it is correctly saving and retrieving the selection as I can close the app and re-open it and have the correct theme row ticked. The only issue is when I go back to the previous view and re-enter the Card Theme view, it is still showing the same selection as from when the app loaded, rather than any changes in selection since the app loaded. Is there a quick fix for that? For example can I incorporate @EnvironmentObject? Thanks.
Nov ’19