swiftui - Picker selection, ForEach an binding property - Stack Overflow

I'm trying to make a view with some pickers. The selections should be connected to a property of a

I'm trying to make a view with some pickers. The selections should be connected to a property of an binding array:

@Binding var limits: [Limit]

var body: some View {

   ForEach(limits) { limit in 
      HStack {
         Text("Value \(limit.value): "
         Text("\(limit.from, specifier: "%.1f")")
         Text("  - ")

         let values = getChangeValuesByLimit(limit: limit)

         Picker("lower", selection: ????) {
            ForEach(values, id:\.self) { value in
               Text("\(value, specifier: "%.1f")").tag(value)
            }
         }
      }
   }
}

I don't know how to save the selection (????). How can I achieve this?

I'm trying to make a view with some pickers. The selections should be connected to a property of an binding array:

@Binding var limits: [Limit]

var body: some View {

   ForEach(limits) { limit in 
      HStack {
         Text("Value \(limit.value): "
         Text("\(limit.from, specifier: "%.1f")")
         Text("  - ")

         let values = getChangeValuesByLimit(limit: limit)

         Picker("lower", selection: ????) {
            ForEach(values, id:\.self) { value in
               Text("\(value, specifier: "%.1f")").tag(value)
            }
         }
      }
   }
}

I don't know how to save the selection (????). How can I achieve this?

Share Improve this question edited Mar 23 at 22:11 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 23 at 22:07 user2836375user2836375 1412 silver badges11 bronze badges 1
  • 1 The picker needs a binding in order to modify/set the value with the selection you make. I doubt you'd want to modify anything inside the limits array, so limits doesn't need to be a Binding. Instead, pass the limits array as a constant that you can use for looping (let limits: [Limit]), and accept a @Binding var selectedLimit: Limit? as a second parameter that you can use in your picker: Picker("lower", selection: $selectedLimit). – Andrei G. Commented Mar 24 at 0:38
Add a comment  | 

1 Answer 1

Reset to default 0

Try this approach using ForEach($limits) { $limit in ...} and Picker("lower", selection: $limit.from) {...}

Since you don't provide a complete code sample, I had to make some assumptions regarding Limit, such as struct Limit: Identifiable, Hashable ... for example. Similarly with what the function getChangeValuesByLimit(...) returns.

Note of importance, you must have the Picker selection the same type as the .tag(value).

Example code:



struct YourView: View {
    @Binding var limits: [Limit]

    var body: some View {
        ForEach($limits) { $limit in
            HStack {
                Text("Value \(limit.value): ")
                Text("\(limit.from, specifier: "%.1f")")
                Text("  - ")

                let values = getChangeValuesByLimit(limit: limit)

                Picker("lower", selection: $limit.from) {
                    ForEach(values, id: \.self) { value in
                        Text("\(value, specifier: "%.1f")")
                            .tag(value)
                    }
                }
            }
        }
    }

    func getChangeValuesByLimit(limit: Limit) -> [Double] {
        return [0.0,1.0,2.0,3.0]
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744267211a4565922.html

相关推荐

  • swiftui - Picker selection, ForEach an binding property - Stack Overflow

    I'm trying to make a view with some pickers. The selections should be connected to a property of a

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信