0
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")
    }
}

I have a settings menu where the user picks a theme, the default value is set to a global variable, globalVarTheme, which is 0. But after they make a selection, exit that menu, and re-enter the menu it goes back to 0 (the first item) even if they have chosen one of the other items. How do I save their selection?

Also, what is the best way to save user selections beyond the current app session? Should I write all their selections to a plist file or is there a conventional way?

alamodey
  • 14,320
  • 24
  • 86
  • 112
  • Does this answer your question? [How can I use UserDefaults in Swift?](https://stackoverflow.com/questions/31203241/how-can-i-use-userdefaults-in-swift) – amq Nov 12 '19 at 11:45

2 Answers2

0

@State is used for the changes within a given view. It is not meant to persist across the views. Instead use @Environment property wrapper. WWDC 2019 talks about that when to use what.

Partha G
  • 1,056
  • 8
  • 13
0

@State isn't the right PropertyWrapper.

If you want to use your settings in multiple views than use @EnvironmentObject as the PropertyWrapper.

You can read about the different PropertyWrappers here: https://medium.com/@alex.hsieh/state-objectbinding-and-environmentobject-in-swiftui-783588b60671

If you want to save the Settings beyond the current app session you can use UserDefauls. How do I use UserDefaults with SwiftUI?

KevinP
  • 2,562
  • 1
  • 14
  • 27
  • I have updated my code to use UserDefaults, and I can see the selection being saved between app sessions. But I can't get the selection to persist between views, so I suspect I need to use EnvironmentObject. Do you mind showing me what modification I need to make? – alamodey Nov 16 '19 at 03:53