首页 > 解决方案 > Settings view in 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.

标签: iosswift

解决方案


假设您有一个带有开关的视图,您可以在您的应用程序的一个名为UserDefaultsdocumented here的部分中更新一个标志。然后,当您访问应用程序中显示温度的部分时,您可以检查您设置的标志以查看用户是喜欢摄氏温度还是华氏温度,并相应地显示正确的视图。

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


推荐阅读