I am trying to get a value from a picker and store it so it can be passed into the function...
The problem is that I try to pass the Picker value into a function as the action for a button, then covert it to an integer, but it just uses the default value, and it seems like the it is never reading the value from the picker
Here is my ContentView file for reference:
struct ContentView: View {
@State var repetitions: String
let directions : [String] = ["Left", "Right"]
var body: some View {
VStack {
Picker("Values", selection: $repetitions) {
ForEach((0...100), id: \.self) {
Text("\($0)")
}
}
Button(action: {choseDirection(reps: repetitions)}, label: { // value passed into function here
Text("Start Drill")
.frame(minWidth: 0, maxWidth: 200)
.padding()
.foregroundColor(.white)
.background(
RoundedRectangle(cornerRadius: 10)
.stroke(lineWidth: 2)
.background(Color.blue.cornerRadius(10))
)
})
}
}
func choseDirection(reps: String) {
let reps = Int(reps) ?? 1 // HERE always uses default value instead of value from picker
var count = 1
while count <= reps {
// does some action
}
}
}
So how can I change this to read the value, so it can be used in the function for the correct amount of repetitions