嗨,我有一个带有最小值和最大值的多选取器,如果用户将最小值放在最大值上,我想要将最小值的选择设置为最大值,这样用户就不能使用最小值而不是最大值。但我不知道如何在会话期间更改选择器的选择。
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)
}
}
}有人能帮帮我吗?
发布于 2020-07-18 05:41:04
我已经解决了这个问题:
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]
}
}
}https://stackoverflow.com/questions/62940364
复制相似问题