简体   繁体   中英

SwiftUI passing keys and values in Form Picker

Where can I pass my values to the update my barChart instead of the Keys updating the barChart. I want the Picker to still display the keys on the Picker Label but the barChart I want to be updated by scoreModel.eventMDL.values the scoreModel is a dictionary eventMDL = [Double: Double,]

class UpdateScoreBar: ObservableObject {
        @Published var rawScoreMDL = 0.0
    }

> This is the main view that passes in RawScoreView

    struct ScoreCalcView: View {
        @ObservedObject private var updateScoreBar = UpdateScoreBar()

        var body: some View {
            ZStack {
                Color("appBackround").edgesIgnoringSafeArea(.all)
                VStack {
                    RawScoreView()
                }.animation(.default)
            }
        }
    }
    struct ScoreBarView: View {


> This is the initial value for the barChart

        var value: CGFloat = 0

        var body: some View {
            VStack{
                ZStack (alignment: .bottom) {
                    Capsule().frame(width: 34, height: 200)
                        .foregroundColor(Color("barColorBackround"))
                    Capsule().frame(width: 34, height: value)
                        .foregroundColor(Color.red)
                    Capsule().frame(width: 30, height: value)
                        .foregroundColor(Color("barColorForeground"))
                }
            }
        }
    }
    struct RawScoreView: View {
        @ObservedObject private var updateScoreBar = UpdateScoreBar()
        @ObservedObject private var scoreModel = ScoreModel()

        var body: some View {
            VStack {
                HStack {

>  updates the view, here is where im trying to pass in the
> scoreModel.eventMDL.values

                    ScoreBarView(value: CGFloat(updateScoreBar.rawScoreMDL))
                }
                NavigationView {
                    Form {
                        Section {

> this binds the selection and updates the view

                            Picker(selection: $updateScoreBar.rawScoreMDL, label: Text("3-Rep Max Deadlift")) {
                                List(self.scoreModel.eventMDL.keys.sorted().reversed(), id: \.self) { i in
                                    Text("\(i, specifier: "%g") Lbs.")
                                }
                            }
                        }
                    }.navigationBarTitle(Text("Raw Score"), displayMode: .inline)
                }
            }
        }
    }

I think the only thing you need to change to get what you're wanting is:

ScoreBarView(value: CGFloat(scoreModel.eventMDL[updateScoreBar.rawScoreMDL] ?? 0))

The Picker 's binding will update updateScoreBar.rawScoreMDL , and you use that key to look up the corresponding value in the scoreModel.eventMDL dictionary. Although it should never be nil , we provide a default value of 0, just in case, convert to CGFloat, and pass that to ScoreBarView . Because rawScoreMDL is a @Published variable, the score bar will update automatically whenever you make a new selection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM