Swiftui Multi Picker 使用 min max 更改选择
Swiftui Multi Picker change selection with min max
您好,我有一个带有最小值和最大值的多选择器,如果用户将最小值放在最大值之上,我想做出反应,我想将最小值的选择设置为最大位置,这样用户就不能最小值超过最大值。但是我不知道如何在会话期间更改选择器的选择。
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 { "\([=10=])" }),
("Max", Array(2...60).map { "\([=10=])" })
]
@State var selection: [String] = [3, 10].map { "\([=10=])" }
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)
}
}
}
有人能帮帮我吗?
我已经解决了:
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]
}
}
}
您好,我有一个带有最小值和最大值的多选择器,如果用户将最小值放在最大值之上,我想做出反应,我想将最小值的选择设置为最大位置,这样用户就不能最小值超过最大值。但是我不知道如何在会话期间更改选择器的选择。
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 { "\([=10=])" }),
("Max", Array(2...60).map { "\([=10=])" })
]
@State var selection: [String] = [3, 10].map { "\([=10=])" }
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)
}
}
}
有人能帮帮我吗?
我已经解决了:
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]
}
}
}