gpt4 book ai didi

ios - View 仅在应用程序重启后由 EnvironmentObject 更新

转载 作者:行者123 更新时间:2023-12-05 02:03:49 24 4
gpt4 key购买 nike

我有这种奇怪的行为,我已经尝试修复了好几个小时:

当 EnvironmentObject UserSettings 更改时,我的一个 View 未更新。尽管如果我完全重新启动应用程序,新值将按原样使用。

这是一个简短的 gif:https://s8.gifyu.com/images/Screen-Recording-2020-11-17-at-15.36.07.gif

所以值被正确存储并且 View 正在访问正确的实例但是因为它是@Published,我希望它像在另一个 View 中一样立即改变。知道我做错了什么吗?

我构建了一个显示相同行为的小应用程序:

用户设置.swift

import SwiftUI

final class UserSettings: ObservableObject {

@Published var colorSelection: Int {
didSet {
UserDefaults.standard.set(colorSelection, forKey: "colorSelection")
}
}

@Published var qnh: Double {
didSet {
UserDefaults.standard.set(qnh, forKey: "qnh")
}
}

@Published var offset: Double {
didSet {
UserDefaults.standard.set(offset, forKey: "offset")
}
}

@Published var colors: Array<Color> {
didSet {
UserDefaults.standard.set(colors, forKey: "colors")
}
}

init() {
self.colorSelection = UserDefaults.standard.object(forKey: "colorSelection") as? Int ?? 0
self.qnh = UserDefaults.standard.object(forKey: "qnh") as? Double ?? 1013.25
self.offset = UserDefaults.standard.object(forKey: "offset") as? Double ?? 0
self.colors = UserDefaults.standard.object(forKey: "colors") as? Array ?? [Color.green, Color.white, Color.red, Color.blue, Color.orange, Color.yellow, Color.pink, Color.purple, Color.black]
}
}

TestApp.swift

import SwiftUI

@main
struct TestApp: App {
@StateObject var userSettings = UserSettings()

var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(UserSettings())
}
}
}

ContentView.swift

import SwiftUI

struct ContentView: View {
@EnvironmentObject var userSettings: UserSettings
@State var view = 2

var body: some View {
TabView(selection: $view) {
MainView()
.environmentObject(UserSettings())
.tabItem {Image(systemName: "location")}
.tag(0)
NavigationView {
SettingsView()
.environmentObject(UserSettings())
.navigationTitle("Settings")
}
.tabItem {Image(systemName: "gearshape")}
.tag(2)
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(UserSettings())
}
}

主视图.swift

import SwiftUI

struct MainView: View {
@EnvironmentObject var userSettings: UserSettings

var body: some View {
Text("Test")
.foregroundColor(userSettings.colors[userSettings.colorSelection])
}
}

设置 View .swift

import SwiftUI

struct SettingsView: View {
@EnvironmentObject var userSettings: UserSettings
@State private var selection: Int = 0
private let colors = ["Green", "White", "Red", "Blue", "Orange", "Yellow", "Pink", "Purple", "Black"]

var body: some View {
Form {
Section(header: Text("Customize")) {
Picker(selection: $userSettings.colorSelection, label: Text("Font Color")) {
ForEach(0 ..< colors.count) {
Text(self.colors[$0]).foregroundColor(userSettings.colors[$0])
}.foregroundColor(userSettings.colors[userSettings.colorSelection])
}
}
}
}
}

最佳答案

您正在创建 UserSetting 的新对象并将它们传递给 subview 。您应该创建该对象并将其传递给 subview 。

在您的内容 View 中

struct ContentView: View {
@EnvironmentObject var userSettings: UserSettings
@State var view = 2

var body: some View {
TabView(selection: $view) {
MainView()
.environmentObject(userSetting) //<< here pass the correct one
.tabItem {Image(systemName: "location")}
.tag(0)
NavigationView {
SettingsView()
.environmentObject(userSetting) //<< here aswell
.navigationTitle("Settings")
}
.tabItem {Image(systemName: "gearshape")}
.tag(2)
}
}
}

编辑:

它实际上更简单,因为您不必手动将其传递给 subview 。只需声明

@EnvironmentObject var userSettings: UserSettings

在您的 View 中,您可以访问它,而无需传递它。

关于ios - View 仅在应用程序重启后由 EnvironmentObject 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64877586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com