【发布时间】:2021-03-13 05:51:43
【问题描述】:
我对 Swift 和 SwiftUI 还很陌生,所以这可能有一些我忽略的明确答案。我的目标是每次单击按钮时都会更新此进度环。我希望按钮将DailyGoalProgress 更新某个数字,这很有效。但是,我还希望 DailyGoalProgress 然后除以一个数字来得到这个 @State。正如我所说,DailyGoalProgress 更新但@State 没有,我无法弄清楚我做错了什么。任何帮助是极大的赞赏。谢谢!
这是我的代码的缩写版本:
struct SummaryView: View {
@AppStorage ("DailyGoalProgress") var DailyGoalProgress: Int = 0
@State var progressValue: Float = 0 //This just stays at zero and never changes even with the button press
var body: some View {
ProgressBar(progress: self.$progressValue, DailyGoalProgress: self.$DailyGoalProgress)
}
}
这是另一种观点:
@Binding var DailyGoalProgress: Int
@Binding var progressValue: Float
var body: some View {
Button(action: {DailyGoalProgress += tokencount; progressValue = Float(DailyGoalProgress / 30)}) {
Text("Mark This Task As Completed")
.font(.title3)
.fontWeight(.semibold)
.foregroundColor(Color.white)
}
}.frame(width: 330.0, height: 65.0).padding(.bottom, 75.0)
Spacer()
}.padding(.top, 125.0)
}
Spacer()
}
}
}
【问题讨论】:
-
您是否需要两个不同的变量来跟踪进度?你可以单独使用@AppStorage。
-
@TusharSharma 我也单独显示
DailyGoalProgress,所以是的,我两者都需要。