Swiftui Multi Picker 以最小最大值更改选择
Posted
技术标签:
【中文标题】Swiftui Multi Picker 以最小最大值更改选择【英文标题】:Swiftui Multi Picker change selection with min max 【发布时间】:2020-07-16 17:31:59 【问题描述】:您好,我有一个具有最小值和最大值的多重选择器,如果用户将最小值置于最大值之上,我想做出反应,我想将最小值的选择设置为最大值位置,这样用户就无法使用最小值超过最大值。但我不知道如何在会话期间更改选择器的选择。
struct MultiPicker: View
typealias Label = String
typealias Entry = String
let data: [ (Label, [Entry]) ]
@Binding var selection: [Entry]
var body: some View
GeometryReader geometry in
HStack
ForEach(0..<self.data.count) column in
Picker(self.data[column].0, selection: self.$selection[column])
ForEach(0..<self.data[column].1.count) row in
Text(verbatim: self.data[column].1[row])
.tag(self.data[column].1[row])
.pickerStyle(WheelPickerStyle())
.frame(width: geometry.size.width / CGFloat(self.data.count), height: geometry.size.height)
.clipped()
struct MultiPickerView: View
@State var data: [(String, [String])] = [
("Min", Array(1...30).map "\($0)" ),
("Max", Array(2...60).map "\($0)" )
]
@State var selection: [String] = [3, 10].map "\($0)"
var body: some View
VStack(alignment: .center)
Text(verbatim: "Selection: \(selection)")
HStack
Text(data[0].0)
.frame(maxWidth: .infinity, alignment: .center)
Text(data[1].0)
.frame(maxWidth: .infinity, alignment: .center)
MultiPicker(data: data, selection: $selection).frame(height: 300)
有人可以帮我吗?
【问题讨论】:
【参考方案1】:我已经解决了:
MultiPicker(data: data, selection: $selection).frame(height: 300)
.onReceive([self.selection].publisher.first()) _ in
self.correctMinMaxDays()
private func correctMinMaxDays()
withAnimation
if Int(selection[0])! > Int(selection[1])!
selection[0] = selection[1]
【讨论】:
以上是关于Swiftui Multi Picker 以最小最大值更改选择的主要内容,如果未能解决你的问题,请参考以下文章
iOS 15 SwiftUI 3 Picker 绑定在更改@State 值后不起作用