简体   繁体   English

尝试使用选择器中选择的索引值而不是选择器的值

[英]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.

相关问题 使用带有一系列选项的 swiftui 选择器时。 如何将所选选项而不是索引值保存到 Cloud Firestore? - When using a swiftui picker with an array of options. How can I save the selected option rather than the index value to Cloud Firestore? array.Max();对于数组索引而不是值 - array.Max(); for the array index rather than the value 按值而不是索引对 Perl 数组进行切片的惯用方法 - Idiomatic way to slice a Perl array by value rather than index 在python numpy.savez中使用变量而不是关键字的值 - Use value of variable rather than keyword in python numpy.savez 如何设置选择器标题以将变量float用作初始值? (迅速) - How do I set a picker title to use a variable float as the initial value? (Swift) Ruby:如何使GLib :: Timeout.add_seconds($ second)使用数组而不是一个静态值? - Ruby: How to make GLib::Timeout.add_seconds($second) use an array rather than one static value? 通用对象获取空值而不是双精度值 - Generic Object obtaining null value rather than a double value PHP PDO导致“数组”值而不是实际值 - PHP PDO resulting in “Array” value rather than actual value 指向数组元素的指针,该元素显示内存地址而不是元素的值 - Pointer to Elements of an Array printing the memory address rather than the value of the element jQuery .val返回选项的文本而不是value标记的内容 - jquery .val returning the text of the option rather than the contents of the value tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM