SwiftUI : Picker does not update correctly when changing datasource

前端 未结 4 1153
花落未央
花落未央 2020-12-30 11:40

I have just started learning SwiftUI and got stuck somewhere!

I am trying to change segment styled picker datasource when changing value of another segment. But some

4条回答
  •  孤城傲影
    2020-12-30 12:24

    Nick Polychronakis solved it in this fork: https://github.com/nickpolychronakis/100DaysOfSwiftUI/tree/master/UnitCoverter

    The solution is to add .id(:identifier:) to your picker so it is unique.

    Observable var:

    @State var unit = 0
    

    Main picker:

    Picker("Length", selection: $unit) {
                        ForEach(0 ..< inputUnitTypes.count) {
                            Text("\(self.inputUnitTypes[$0].description)")
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
    

    One of secondary pickers which content is determined by the unit variable.

    Picker("Length", selection: $inputUnit) {
                            ForEach(0 ..< selected.count) {
                                Text("\(self.selected[$0].description)")
                            }
                        }
                        .id(unit)
    

提交回复
热议问题