SwiftUI 在 UserDefaults 中设置字符串值,但作为`EnvironmentObject`
Posted
技术标签:
【中文标题】SwiftUI 在 UserDefaults 中设置字符串值,但作为`EnvironmentObject`【英文标题】:SwiftUI set string value inside UserDefaults but as a `EnvironmentObject` 【发布时间】:2020-02-06 20:19:36 【问题描述】:您好,是否有一种简单的方法可以将 UserDefaults 中的值作为EnvironmentObject
显示在其他视图中?在 SettingsView 中设置用户名并显示在 DashboardView 中。非常感谢
【问题讨论】:
【参考方案1】:终于找到How do I use UserDefaults with SwiftUI?
答案仅基于Bool
。她也是String
的解决方案。
使用 TabView 设置的 ContentView:
import SwiftUI
struct ContentView: View
var body: some View
VStack
TabView
Dashboard(userName: UserName())
.tabItem(
Image(systemName: "rectangle.3.offgrid")
Text("Dashboard")
)
Settings()
.tabItem(
Image(systemName: "gear")
Text("Einstellungen")
)
#if DEBUG
struct ContentView_Previews: PreviewProvider
static var previews: some View
ContentView()
#endif
布尔型的用户设置和字符串的用户名
import SwiftUI
import Combine
final class UserSettings: ObservableObject
let objectWillChange = PassthroughSubject<Void, Never>()
@UserDefault("ShowOnStart", defaultValue: true)
var showOnStart: Bool
willSet
objectWillChange.send()
final class UserName: ObservableObject
let objectWillChange = PassthroughSubject<Void, Never>()
@UserDefault("userName", defaultValue: "UknowName")
var userNameSet: String
willSet
objectWillChange.send()
在 UserSettings 和 UserName 类中使用的属性包装器 UserDefaults
@propertyWrapper
struct UserDefault<T>
let key: String
let defaultValue: T
init(_ key: String, defaultValue: T)
self.key = key
self.defaultValue = defaultValue
var wrappedValue: T
get
return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
set
UserDefaults.standard.set(newValue, forKey: key)
那里的第一个视图仪表板显示了在设置视图中所做的更改
import SwiftUI
struct Dashboard: View
@ObservedObject var settings = UserSettings()
@ObservedObject var userName = UserName()
var body: some View
VStack
Text("hallo")
ExtractedViewName(userName: userName)
ExtractedView(settings: settings)
struct Dashboard_Previews: PreviewProvider
static var previews: some View
Dashboard(userName: UserName())
struct ExtractedView: View
@ObservedObject var settings : UserSettings
var body: some View
VStack
if settings.showOnStart
Text("Welcome")
struct ExtractedViewName: View
@ObservedObject var userName : UserName
var body: some View
Text("Test One: \(userName.userNameSet)")
那里的第二个视图设置设置显示在仪表板视图中的更改
import SwiftUI
struct Settings: View
@ObservedObject var settings = UserSettings()
@ObservedObject var userName = UserName()
@State private var userTextfield = ""
var body: some View
VStack
Toggle(isOn: $settings.showOnStart)
Text("Show welcome text")
TextField("Name", text: $userTextfield)
self.userName.userNameSet = self.userTextfield
Button(action:
print("\(self.userName.userNameSet)")
)
Text("Button")
struct Settings_Previews: PreviewProvider
static var previews: some View
Settings()
【讨论】:
以上是关于SwiftUI 在 UserDefaults 中设置字符串值,但作为`EnvironmentObject`的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Userdefaults (SwiftUI) 中将新元素附加到数组
如何在 SwiftUI 中将 Stepper 数据保存到 UserDefaults?
在 SwiftUI 中使用 UserDefaults 进行全局更新
UserDefaults 在 SwiftUI 中与 Toggle 绑定