ios - 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.
解决方案
假设您有一个带有开关的视图,您可以在您的应用程序的一个名为UserDefaults
documented here的部分中更新一个标志。然后,当您访问应用程序中显示温度的部分时,您可以检查您设置的标志以查看用户是喜欢摄氏温度还是华氏温度,并相应地显示正确的视图。
UserDefaults.standard.set(false, forKey: "celsius")
let preference = UserDefaults.standard.string(forKey: "celcius")
if preference == true {
//user prefers Celsius
} else {
//user prefers Fahrenheit
}
推荐阅读
- swift - 在 SwiftUI 中修改 ForEach 中的结构数据
- automation - 解决 PythonAnywhere 中未找到模块的错误
- python-3.x - Python 子类错误
- javascript - 云功能中未定义的 GCP VM 网络接口元数据
- c++ - C++中的非成员函数和全局函数是一样的吗?
- ios - Cocoapods:[!] CocoaPods 找不到 pod "Firebase/Storage" 的兼容版本:
- swift - 如何向用户刷新问题?
- vb.net - [VB.NET]如何解析这个html?
- html - 修复了下拉菜单,但 css 样式妨碍了
- javascript - Logcat 错误和应用程序不断因 android.view.WindowManager$BadTokenException 而崩溃?