最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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]
    }
}

发布评论

评论列表(0)

  1. 暂无评论