我正在尝试使用onTapGesture
,但这样做几乎使访问列表变得不可能-如果我删除了onTapGesture
,它将可以正常工作
这就是我正在使用的东西:
struct NavPickerView: View {
@State var selected: Int = -1
@State var cities: [String] = ["New York", "San Diego", "Houston", "Boston"]
@State var selectionStarted: Bool = false
var body: some View {
VStack {
Text("\(selectionStarted ? "SELECTING" : "WAITING")")
NavigationView {
Form {
Section {
Picker(selection: $selected, label: Text("Search for...")) {
Text("Select City").tag(-1)
ForEach(0..<cities.count) {
Text(verbatim: self.cities[$0]).tag($0)
}
}
}
}
.onTapGesture { self.selectionStarted = true }
.navigationBarTitle(Text("Cities"))
}
}
}
}
在选择一项而不使列表几乎不可访问时运行代码的任何方式吗?
答案 0 :(得分:0)
对于您来说,.onAppear
似乎更合适:
Form {
Section {
Picker(selection: $selected, label: Text("Search for...")) {
Text("Select City").tag(-1)
ForEach(0..<cities.count) {
Text(verbatim: self.cities[$0]).tag($0)
}
.onAppear { self.selectionStarted = true }
}
}
}