[英]@State not updating SwiftUI
I'm pretty new to Swift and SwiftUI, so this might have some clear answers that I'm overlooking.我对 Swift 和 SwiftUI 很陌生,所以这可能有一些我忽略的明确答案。 My goal is to have this progress ring update every time a button is clicked.
我的目标是每次单击按钮时都会更新此进度环。 I want the button to update
DailyGoalProgress
by a certain number, and that works.我希望按钮将
DailyGoalProgress
更新某个数字,这很有效。 However, I also want DailyGoalProgress
to then be divided by a number to make this @State
.但是,我还希望
DailyGoalProgress
然后除以一个数字来生成这个@State
。 As I said, the DailyGoalProgress updates but the @State
does not and I can't figure out what I'm doing wrong.正如我所说,DailyGoalProgress 更新但
@State
没有,我无法弄清楚我做错了什么。 Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks!
谢谢!
Here is an abbreviated version of my code:这是我的代码的缩写版本:
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)
}
}
Here is another view:这是另一种观点:
@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()
}
}
}
Your problem is about Int to Float , DailyGoalProgress is Int and you should convert it to Float first then divid it to 30.0, then it will work.您的问题是关于Int 到 Float , DailyGoalProgress是Int ,您应该先将其转换为Float然后将其除以 30.0,然后它会起作用。
It called Inference, Swift wants help you in its own way.它称为推理,Swift 想以自己的方式帮助你。 but it makes problem to you.
但这给你带来了麻烦。
if you want detail what happens: when you divid DailyGoalProgress to 30 what ever the result is it will taken as Int, then what does mean?如果您想详细了解会发生什么:当您将 DailyGoalProgress 除以 30 时,结果是什么,它将被视为 Int,那是什么意思? that mean number between 0 to 1 became always 0, then you gave 0 to Float and also nothing happens!
0 到 1 之间的平均数总是 0,然后你给 Float 0,也没有任何反应!
import SwiftUI
struct ContentView: View {
@AppStorage ("DailyGoalProgress") var DailyGoalProgress: Int = 0
@State var progressValue: Float = 0
var body: some View {
Spacer()
Text(DailyGoalProgress.description)
Text(progressValue.description)
ProgressBar(DailyGoalProgress: $DailyGoalProgress, progressValue: $progressValue)
Spacer()
}
}
struct ProgressBar: View {
@Binding var DailyGoalProgress: Int
@Binding var progressValue: Float
let tokencount: Int = 30 // <<: Here for example I used 30
var body: some View {
Button(action: {
DailyGoalProgress += tokencount
progressValue = Float(DailyGoalProgress)/30.0 // <<: Here
}, label: {
Text("Mark This Task As Completed")
.font(.title3)
.fontWeight(.semibold)
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.