swiftuiswiftui-pickerswiftui-state

@State var not changing - SwiftUI picker


I am a real newbie in swift, but I am stuck at this problem... I am building a picker to change an optical prescription, but I am not able to change SPH and CYL values (doubles), while AX works good. Any help? Also the way that text comes out, I tried to add .format modifier just to show 2 decimals, but still no luck. Thanks!

import SwiftUI

struct ContentView: View {

@State var selectedsph = 0.0
@State var selectedcyl = 0.0
@State var selectedax = 0

var sph : [Double] = Array(stride(from: -20.00, through: 20.00, by: 0.25))
var cyl : [Double] = Array(stride(from: -10.00, through: 10.00, by: 0.25))
var ax = [Int](0...180)


var body: some View {
    GeometryReader { geometry in
        VStack {
            Spacer()
            HStack(spacing:0) {
                
                Picker(selection: self.$selectedsph, label: Text("")) {
                    ForEach(0 ..< self.sph.count) { index in
                        Text("Sph  " + "\(self.sph[index])").tag(index)
                    }
                }
                .pickerStyle(.wheel)
                .frame(width: geometry.size.width/3, height: 150)
                .compositingGroup()
                .clipped()
                
                Picker(selection: self.$selectedcyl, label: Text("Picker")) {
                    ForEach(0 ..< self.cyl.count) { index in
                        Text("Cyl  " + "\(self.cyl[index])").tag(index)
                    }
                }
                .pickerStyle(.wheel)
                .frame(width: geometry.size.width/3.5, height: 150)
                .compositingGroup()
                .clipped()
                
                Picker(selection: self.$selectedax, label: Text("Picker")) {
                    ForEach(0 ..< self.ax.count) { index in
                        Text("AX  " + "\(self.ax[index])").tag(index)
                    }
                }
                .pickerStyle(.wheel)
                .frame(width: geometry.size.width/3, height: 150)
                .compositingGroup()
                .clipped()
                
            }.padding()
        }
        HStack(alignment: .center) {
            Text("Occhio Destro: SF: \(selectedsph)  Cyl: \(selectedcyl)  Ax: \(selectedax)")
                .fontWeight(.medium).multilineTextAlignment(.center).padding(.all)
            
        }
        Spacer()
        
    }
  }
}

Solution

  • The picker values are the indices of the selected values. Change your @State vars to hold Int:

    @State var selectedsph = 0
    @State var selectedcyl = 0
    @State var selectedax = 0
    

    And change your Text to use the indices to look up the values. Add specifier: "%.2f" to show just 2 decimal places:

    Text("Occhio Destro: SF: \(sph[selectedsph], specifier: "%.2f")  Cyl: \(cyl[selectedcyl], specifier: "%.2f")  Ax: \(ax[selectedax])")
                .fontWeight(.medium).multilineTextAlignment(.center).padding(.all)