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
Picker doesn't show the preferred language (I'm sure the enviroment object has the property setted
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