gpt4 book ai didi

ios - 从选择器中获取选定的项目 | swift 用户界面

转载 作者:行者123 更新时间:2023-12-05 09:31:20 26 4
gpt4 key购买 nike

我想从选择器中获取所选项目以更新 Firebase 数据库中的一些数据,但是当我使用 onTapGesture 时不起作用

注意:选择器中的项目是字符串

我的代码:

            Picker(selection: $numUnitIndex, label: Text("Numerical Unit: \(numUnit)")) {
ForEach(0 ..< units.count) {
Text(self.units[$0]).tag($0).foregroundColor(.blue)
.onTapGesture {
//updateUnit(newUnit: self.units[numUnitIndex])
print("selected \(numUnitIndex)")
}
}
}.pickerStyle(MenuPickerStyle())

最佳答案

这里是一个简单的正确方法的例子,这里不需要onTapGesture:

struct ContentView: View {

let units: [String] = ["😀", "😎", "🥶", "😡", "😍"]
@State private var selectedUnit: Int = 0

var body: some View {

Picker(selection: $selectedUnit, label: Text("You selected: \(units[selectedUnit])")) {
ForEach(units.indices, id: \.self) { unitIndex in Text(units[unitIndex]) }
}
.pickerStyle(MenuPickerStyle())
.onChange(of: selectedUnit, perform: { newValue in print("Selected Unit: \(units[newValue])", "Selected Index: \(newValue)")})
}
}

关于ios - 从选择器中获取选定的项目 | swift 用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68867876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com