[英]Attempting to use value of index selected in picker rather than value of picker
I'm attempting to figure out how I can use the value of the index in my picker for an equation, rather than usng the selected number of the picker.我试图弄清楚如何将选择器中的索引值用于方程式,而不是使用选择器的选定数字。 I am not having success我没有成功
Following is my array:以下是我的数组:
var wheelHeights = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70]
User, via picker, will select their preferred wheel height above the ground.用户将通过选择器选择他们喜欢的离地高度。
My code then goes through a few (a lot actually) if statement sections, and finally to my issue...然后我的代码经历了一些(实际上很多)if 语句部分,最后是我的问题......
var wheelHeightFactor: Double {
let wheelFactor = Double(wheelHeight)
let densityWF = Double(densityWeightFactor)
if wheelFactor <= 21 && densityWeightFactor >= 2.9 {
let whf = Double((wheelFactor) * 0.2) + densityWeightFactor
return whf
I'm looking to make wheelHeight in this equation be the picker index value, rather than the actual wheelHeight.我希望让这个方程中的wheelHeight 成为选择器索引值,而不是实际的wheelHeight。
Following is what the code does now:以下是代码现在所做的:
let whf = Double((15) * 0.2) + densityWeightFactor
return whf
The equation SHOULD look like this in terms of where my numbers are if user selected 15:如果用户选择了 15,就我的数字在哪里而言,等式应该是这样的:
let whf = Double((3) * 0.2) + densityWeightFactor
return whf
Thank you all in advance谢谢大家
You will have to create a seperate @State
variable to keep track of the actual index:您必须创建一个单独的@State
变量来跟踪实际索引:
Consider the following example:考虑以下示例:
struct ContentView: View {
let data = [1,5,10,20]
@State private var selectedIndex = 0
var body: some View {
VStack {
Text("Current index: \(selectedIndex)")
Picker("Your Selection", selection: $selectedIndex) {
ForEach(0..<data.count) { index in
Text("\(data[index])")
}
}
}
}
}
So if I understand your example right, then you want to use the actual index instead of wheelFactor
.因此,如果我正确理解您的示例,那么您想使用实际索引而不是wheelFactor
。 If so, you could do something like this:如果是这样,你可以这样做:
@State private var selectedWheelHeightIndex = 0
//...
let whf = Double((selectedWheelHeightIndex) * 0.2) + densityWeightFactor
return whf
(I assume that you are using SwiftUI)... (我假设您使用的是 SwiftUI)...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.