Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios @状态未更新SwiftUI_Ios_Swift_Swiftui_State - Fatal编程技术网

Ios @状态未更新SwiftUI

Ios @状态未更新SwiftUI,ios,swift,swiftui,state,Ios,Swift,Swiftui,State,我对Swift和SwiftUI还很陌生,所以这可能有一些我忽略的清晰答案。我的目标是在每次单击按钮时更新此进度环。我想让按钮按一定的数字更新DailyGoalProgress,这样就行了。但是,我还希望将DailyGoalProgress除以一个数字,使其成为@State。正如我所说的,DailyGoalProgress会更新,但是@State不会更新,我也不知道我做错了什么。非常感谢您的帮助。谢谢 以下是我的代码的缩写版本: struct SummaryView: View {

我对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()
        }
    }
}

您的问题是关于Int到FloatDailyGoalProgressInt,您应该先将其转换为Float,然后再将其除以30.0,然后它就会工作

这叫推理!斯威夫特想以自己的方式帮助你,但它给你带来了麻烦。

如果你想知道具体发生了什么:当你把DailyGoalProgress除以30,结果会被取为Int,那么这意味着什么?0到1之间的平均数总是0,然后你给了0让它浮动,而且什么也没发生

导入快捷界面
结构ContentView:View{
@AppStorage(“DailyGoalProgress”)变量DailyGoalProgress:Int=0
@状态变量progressValue:浮点=0
var body:一些观点{
垫片()
文本(DailyGoalProgress.description)
文本(progressValue.description)
ProgressBar(DailyGoalProgress:$DailyGoalProgress,progressValue:$progressValue)
垫片()
}
}
结构进度栏:视图{
@绑定变量DailyGoalProgress:Int
@绑定值:Float

让tokencount:Int=30//您需要两个不同的变量来跟踪进度吗?您可以单独使用@AppStorage。@TusharSharma我也会自己显示
DailyGoalProgress
,所以是的,我需要这两个变量。
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)

        })
        
 
    }
    
}