Settings view in Swift

时间:2019-04-16 22:19:23

标签: ios swift

So I am working on a simple weather app and I want to add a view where the user can switch between Celsius and Fahrenheit. How would I do this? How would I make it so the switches on that screen would effect the rest of the app? I really don't know where to start with this one. Anything that I have found so far has either been outdated or been using the iOS settings and I don't want to do that. Any help would be great.

1 个答案:

答案 0 :(得分:1)

Assuming you have a view with a switch on it, you could update a flag in a part of your app called UserDefaults documented here. Then, when you visit parts of your app that display the temperature, you can check the flag you have set to see if the user prefers Celsius or Fahrenheit and show the correct view accordingly.

UserDefaults.standard.set(false, forKey: "celsius")
let preference = UserDefaults.standard.string(forKey: "celcius")
if preference == true {
     //user prefers Celsius
} else {
     //user prefers Fahrenheit
}