Make a "default picker" choice

Hi

At the moment i’m trying to build a Golf application, where one of the first steps is for the user to choose course and and combination ( 18 holes, 9 holes etc). Since most players play the same course over and over, i would like the application to start the picker, the same place as the selected last time.

What is the best way to do this ?? (It is only the first picker with the string array i want to save)

The simple picker system is build as below:

import SwiftUI

struct CourseView: View {
    @Binding var klub:String
    @State var klubber:[String] = ["Gilleleje Golf Klub", "Hillerød Golf Klub", "Hørsholm Golf Klub", "Rungsted Golf Klub", "Jokes"]
    @Binding var valgtbane: String
    @State var dataService = DataService()
    @State var holeInfo:[HoleDefinition] = [HoleDefinition]()
    var body: some View {
        
        
        ZStack {
            Color(red: 0.639, green: 0.847, blue: 0.792)
                .ignoresSafeArea()
            
            VStack {
                
                Image("BG")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .cornerRadius( 20)
                    .padding(.all)
                
                Spacer()
                
                Text("Velkommen til BaneGuiden.DK")
                    .font(.largeTitle)
                    .fontWeight(.semibold)
                    .foregroundColor(Color(red: 0.506, green: 0.003, blue: 0.502))
                    .padding()
                
                Spacer()
                
                HStack{
                    Text("Vælg klub")
                    
                    Spacer()
                    
                    Picker("Hvilken Klub", selection: $klub) {
                        Text("<Intet Valgt>").tag("")
                        ForEach(klubber, id: \.self){
                            Text($0)
                            
                        }
                        
                    }
                    
                    
                    
                }.font(.title2)
                
                
                HStack{
                    
                    Text("Vælg kombination")
                    
                    Spacer()
                    
                    
                    let baner:[CourseDefinition] = dataService.updateBaner(input1: klub)
                    Picker("Hvilken kombination", selection: $valgtbane) {
                        Text("<Intet Valgt>").tag("")
                        ForEach(baner, id: \.course){ item in
                            Text("\(item.course)")
                        }
                    }
  //
                    
                }.font(.title2)
                Spacer()
                Text("Din valgte runde er: ")
                Text(klub + " - " + valgtbane)
                Spacer()
               
                
            }.padding()
            
            
        }
        
    }
    
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        CourseView(klub: Binding.constant(""), valgtbane: Binding.constant(""))
    }
}

Hi @Loepp

I don’t speak Spanish but google Translate is helpful.

I guess this is the Picker you are referring to.

                    Picker("Hvilken Klub", selection: $klub) {
                        Text("<Intet Valgt>").tag("")
                        ForEach(klubber, id: \.self){
                            Text($0)
                            
                        }

Since klub is where you are storing your Picker selection, what you could do is define that with the @AppStorage property wrapper which will always save the last klub that was selected.

In your CourseView you have defined that as a @Binding so wherever it is defined as a @State var… change that to @AppStorage("klub") var klub: String = "". That stores it in a special User Defaults location within the App as a key value pair with the key being “klub” and the value being klub.

Here is a description of @AppStorage from Paul Hudson.
https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-appstorage-property-wrapper

1 Like

Thanks Chris
Works like a charm !!

/Loepp

1 Like

Awesome to hear it’s working. :+1: