0

I've a problem with Picker selection when I use my EnviromentObject. My enviroment Object is UserSettings where users save them preferences, like language, currency and so on. I added this object in the the contentview, as enviroment object, declared in scenedelegate. The UserSettings struct has a Picker. My goal is show as first picker selection the choosed settings language but I have 2 problems: 1) the first selection doesn't work 2) when I select on item of list in picker, automatically the view is dismiss up to main view. Let's say view A (main view), view B is the view where picker is, view C is the view when I tap on my picker. When I tap the simulator automatically goes from C-->B (ok) and after 1 second from B-->A. This is my code. Any suggestions will be appreciated. Thank you

class UserSettings : ObservableObject  {
    @Published var linguaCode = ""
    @Published var lingua = ""
    @Published var formatoData = ""
    @Published var formatoValuta = ""
    
    init(){
        
        let linguaChoosed = UserDefaults.standard.string(forKey: "lingua")
        if( linguaChoosed == nil || linguaChoosed!.isEmpty ){
            self._linguaCode =  .init(initialValue: String(Locale.preferredLanguages[0].prefix(2)))
        }
        else{
            self._linguaCode = .init(initialValue: String(linguaChoosed!))
        }
        
        let formatoDataChoosed = UserDefaults.standard.string(forKey: "data")
        if(formatoDataChoosed == nil || formatoDataChoosed!.isEmpty){
            self._formatoData = .init(initialValue: String(GlobalCostants.Data.data_formato_dd_mm_yyyy))
        }
        else {
            self._formatoData = .init(initialValue: String(formatoDataChoosed!))
        }
        
        let valutaChoosed = UserDefaults.standard.string(forKey: "valuta")
        if(valutaChoosed == nil || valutaChoosed!.isEmpty){
            self._formatoValuta = .init(initialValue: String(GlobalCostants.Valuta.valuta_euro_nome))
        }
        else {
            self._formatoValuta = .init(initialValue: String(valutaChoosed!))
        }
        self._lingua = .init(initialValue: getLanguageFromCode(languageCode: linguaCode))
    }
    
    func setLanguageCode(languageCode: String) {
        linguaCode = languageCode
    }
    
    func setCurrency(currencyCode: String) {
        formatoValuta = currencyCode
    }
    func setData(dataFormat: String) {
        formatoData = dataFormat
    }
    func getLanguageFromCode(languageCode: String) -> String {
        switch languageCode {
        case EnumLingua.italiano.rawValue:
            return GlobalCostants.Lingua.lingua_italiano
        case EnumLingua.inglese.rawValue:
            return GlobalCostants.Lingua.lingua_inglese
        default:
            return ""
        }
    }
    
   }

this is my struct:

import Foundation
import SwiftUI

struct prova: View {
    @EnvironmentObject var userSettings : UserSettings


    @State private var listaLingue: [String] = ["italiano","inglese"]

    var body: some View {
       
            Form{
                VStack{
                    
                    Picker(selection: $userSettings.lingua, label: Text(LocalizedStringKey("lingua"))){
                        ForEach(0 ..< listaLingue.count, id: \.self){
                            index in
                            Text(LocalizedStringKey(listaLingue[index])).tag(index)
                            
                        }
                        
                    }
                  
                }
            }
    }
}

Main screen, let say home page

image

Picker doesn't show the preferred language (I'm sure the enviroment object has the property setted

image

When I tap on the picker I see the list and is ok but when I choose one item, the simulator come back automatically to screenshot n° 2 and after 1 second on the main screen n °1

image

pawello2222
  • 46,897
  • 22
  • 145
  • 209
  • 1
    Your `tag()` on the `Picker` has to match the type of `selection`. But, you're using `tag(index)` with an `Int` and `userSettings.lingua` which is a `String` – jnpdx Mar 18 '21 at 17:31
  • This might help you: https://stackoverflow.com/a/64277844/8697793 – pawello2222 Mar 18 '21 at 17:34

1 Answers1

0

Yes, using String as tag instead of Int allows to see the items in the picker but the second problem still remain. When I open the picker and choose one item, let's say english, the picker come back and after 1 second my view comes back to main view. I can't understand why..

I changed picker code too:

                    Picker(selection: $userSettings.lingua, label: Text(LocalizedStringKey("lingua"))){
                    ForEach(0 ..< listaLingue.count, id: \.self){
                        index in
                        Text(LocalizedStringKey(listaLingue[index])).tag(listaLingue[index])
                        
                    }
                    
                }

Cool, now I can see the picker items [1]: https://i.stack.imgur.com/A6Yov.png