英文:
The day counter does not update
问题
我有以下问题。我有两个视图。ProgressCardView 和 BottomSheetView。我的目标是确保当您点击 BottomSheetView 中的 EndSession 按钮时,ProgressCardView 中的值会增加 +1,即天数增加 +1。
问题是当您点击按钮时,数据不会改变。
以下是我的代码。这是 ProgressCardView 中的代码。
struct ProgressCardView: View {
@EnvironmentObject var settings: ProgressSettings
@State private var daysCount = 0
var body: some View {
// ... 这里有一些其他的视图和布局代码 ...
HStack {
Spacer()
Text("\(settings.dayCount) / 30 Days")
.foregroundColor(Color.white)
.font(.system(size: 24, weight: .medium))
.italic()
Spacer()
Text("🌞")
.padding(.trailing)
}
// ... 这里有更多的视图和布局代码 ...
}
}
这是 BottomSheetView 中的代码。
struct BottomSheetView: View {
@Environment(\.dismiss) var dismiss
@StateObject var settings = ProgressSettings()
var body: some View {
// ... 这里有一些其他的视图和布局代码 ...
Button(action: {
settings.dayCount += 1
dismiss()
}) {
Text("End Session")
.frame(width: 230)
.padding()
.foregroundColor(Color.white)
.font(.system(size: 16, weight: .medium))
.italic()
.background(Color("red"))
.cornerRadius(20)
.padding()
}
// ... 这里有更多的视图和布局代码 ...
}
}
以及 ProgressSettings 类。
class ProgressSettings: ObservableObject {
@Published var dayCount = 0
}
希望这能帮助您解决问题。
英文:
I have the following problem. I have two Views. ProgressCardView and BottomSheetView. My goal is to make sure that when you click on the EndSession button in the BottomSheetView, the value changes to +1 in the ProgressCardView, namely the day is increased by +1.
The problem is that the data does not change when you click on the button.
Here is my code. My code in the ProgressCardView.
struct ProgressCardView: View {
@EnvironmentObject var settings: ProgressSettings
@State private var daysCount = 0
var body: some View {
HStack {
VStack(alignment: .leading, spacing: 10) {
HStack {
Text(" \(settings.dayCount) / 30 Days")
.foregroundColor(Color.white)
.font(.system(size: 24, weight: .medium))
.italic()
Spacer()
Text("🏆")
.padding(.trailing)
}
Text("Keep going! You're almost there.")
.font(.system(size: 14, weight: .regular))
.foregroundColor(Color("grey"))
VStack {
ProgressView(value: 0.2)
.padding(.top)
.padding(.bottom)
}
.tint(Color("yellow"))
}
Spacer()
}
.frame(width: 320)
.padding()
.background(Color("backgroundLightSecond"))
.cornerRadius(10)
.padding()
}
struct ProgressCardView_Previews: PreviewProvider {
static var previews: some View {
ProgressCardView()
.environmentObject(ProgressSettings())
}
}
}
My code in BottomSheetView
struct BottomSheetView: View {
@Environment(\.dismiss) var dismiss
@StateObject var settings = ProgressSettings()
var body: some View {
ZStack {
Color("background")
.edgesIgnoringSafeArea(.all)
VStack(alignment: .leading) {
HStack {
Text("Congratulations!")
.padding()
.foregroundColor(Color.white)
.font(.system(size: 24, weight: .medium))
.italic()
Spacer()
Text("🎉")
.padding()
}
Text("Congratulations on the successful completion of today's exercise. I'll see you tomorrow. You did great! See you tomorrow!")
.padding()
.font(.system(size: 14, weight: .regular))
.foregroundColor(Color("grey"))
.lineSpacing(5)
Divider()
.overlay(Color("grey"))
VStack {
HStack {
Spacer()
Button(action: {
settings.dayCount += 1
dismiss()
}) {
Spacer()
Text("End Session")
.frame(width: 230)
.padding()
.foregroundColor(Color.white)
.font(.system(size: 16, weight: .medium))
.italic()
.background(Color("red"))
.cornerRadius(20)
.padding()
Spacer()
}
}
}
}
}
}
struct BottomSheetView_Previews: PreviewProvider {
static var previews: some View {
BottomSheetView()
.environmentObject(ProgressSettings())
}
}
}
and ProgressSettings
class ProgressSettings: ObservableObject {
@Published var dayCount = 0
}
答案1
得分: 0
正如其他人提到的,问题似乎是因为您将 ProgressSettings
注入为 @EnvironmentObject
,但在 BottomSheetView
中使用了一个新实例化的 @StateObject
值。
您需要确保在希望共享此状态的所有位置都使用相同的实例。
例如,在 BottomSheetView
中使用 @EnvironmentObject
,并确保该实例与 ProgressCardView
中的实例相同。
struct BottomSheetView: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject var settings: ProgressSettings
// ...
}
在应用程序的根部仅注入一次实例可以确保只有一个实例会在所有子视图之间共享。
@main
struct MyApp: App {
@StateObject var settings = ProgressSettings()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(settings)
}
}
}
英文:
As others have mentioned, the issue appears to be because you're injecting ProgressSettings
as an @EnvironmentObject
, but you're using a newly instantiated @StateObject
value in BottomSheetView
.
You need to make sure you're using the same instance everywhere you want this state to be shared.
For example, use an @EnvironmentObject
in BottomSheetView
, and make sure the instance is the same as the ProgressCardView
.
struct BottomSheetView: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject var settings: ProgressSettings
// ...
}
Injecting the instance once in the root of the app ensures there's only a single instance that will be shared across all child views.
@main
struct MyApp: App {
@StateObject var settings = ProgressSettings()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(settings)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论