I am new to StackOverflow and SwiftUI programming. So I’ve this quite simple app with a homepage, a settings web page and a web page the place some textual content is displayed (for now).
The factor I want is for the consumer to pick the app language within the settings web page after which:
- the textual content in the entire app to alter accordingly
- the textual content within the third web page (DetailsPage) to point out some textual content with the right translation and the primaryLanguage variable with the right worth (that’s, the identical one which the consumer outlined within the SettingsPage).
I type of get it to work with UserDefaults however once I construct the app and check it, the language doesn’t at all times change and I do not know why and get a bit misplaced between the place to set the UserDefaults and the way to entry it and the way to make sure the info is reloaded after altering it. The concept is to grasp how information persistence works and the way can a variable outlined in a subpage (SettingsPage) be shared with its father or mother web page (the HomePage) and one other subpage of the homepage (DetailsPage).
In fact, the thought is for the settings outlined by the consumer to be maintained in order that when the consumer opens the app again, the identical language that was final outlined stays.
That is my content material view:
import SwiftUI
struct ContentView: View {
var primaryLocale = UserDefaults.commonplace.string(forKey: "primaryLocale")
var physique: some View {
NavigationView {
VStack {
NavigationLink(vacation spot: DetailView()) {
Textual content("Detailed view 1")
}
Spacer()
NavigationLink(vacation spot: SettingsIOSView()) {
Textual content("Settings")
}
Spacer()
let _ = print(primaryLocale)
Textual content(primaryLocale ?? "error")
}
.padding()
.setting(.locale, .init(identifier: primaryLocale ?? "en"))
}
};
}
#Preview {
ContentView()
}
That is the settings web page (not all works):
import SwiftUI
struct SettingsIOSView: View {
@AppStorage("primaryLocale") var primaryLocale = "en"
@AppStorage("primaryLanguage") var primaryLanguage = "English"
@AppStorage("isDarkMode") personal var isDarkMode = false
var physique: some View {
NavigationStack {
Type {
Picker("Column Format", choice: $isDualColumn) {
Textual content("Single Column")
.tag(false)
Textual content("Double Column")
.tag(true)
}
.pickerStyle(.inline)
Picker(choice: $primaryLanguage, label: Textual content("Main Language")) {
Textual content("English").tag("English")
Textual content("Spanish").tag("Spanish")
}
.onChange(of: primaryLanguage) {
if primaryLanguage == "English" {
primaryLocale = "en"
} else if primaryLanguage == "Spanish" {
primaryLocale = "es"
}
}
// let _ = print(primaryLanguage)
Part {
Toggle("Darkish Mode", isOn: $isDarkMode)
}
}
.navigationTitle("Settings")
}
.setting(.locale, .init(identifier: primaryLocale))
}
}
#Preview {
SettingsIOSView()
}
And that is my DetailsPage:
import SwiftUI
struct DetailView: View {
var primaryLanguage = UserDefaults.commonplace.string(forKey: "primaryLanguage")
var physique: some View {
NavigationView {
HStack {
VStack(alignment: .main) {
Textual content("That is some textual content")
Textual content(UserDefaults.commonplace.string(forKey: "primaryLanguage") ?? "Error")
Spacer()
}
// Spacer()
}
.navigationTitle("Single Column Format")
.padding()
.multilineTextAlignment(.main)
}
}
#Preview {
DetailView()
}